[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HomeOfficeViewModel viewModel)
{
return RedirectToAction("SearchResults", "HomeOffice", viewModel);
}
public ActionResult SearchResults(HomeOfficeViewModel viewModel)
{
if (viewModel.FirstName != null && viewModel.LastName == null && viewModel.FullSsn == null)
{
List<Domain.Model.PolicyHolder> ph = _policyHolderRepository.Where(x => x.FirstName == viewModel.FirstName).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId
};
TempData["SearchedItem"] = "<<< First Name >>> is '" + viewModel.FirstName + "'";
return View("SearchResults", new SearchResultsViewModel() {SearchResults = searchresults.ToList()});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "First Name searched does not exist in our records");
return View("Index");
}
}
else
{
return View();
}
}
viewModel中的值显示在url中,如此
http://sample.url.com/HomeOffice/SearchResults?FirstName=testing
我不应该在网址中显示它们,因为我将发送ssn。有没有办法隐藏它们或更好的方法来做到这一点。
感谢。
答案 0 :(得分:2)
RedirectToAction
将为指定的操作(在您的情况下为SearchResults
)创建一个GET请求,这可能会尝试序列化您的视图模型字段。相反,您可以使用TempData
[HttpPost]
public ActionResult Index(HomeOfficeViewModel viewModel)
{
TempData["Field1"] = "Value1";
TempData["HomeOfficeViewModel1"] = viewModel;
return RedirectToAction("SearchResults", "HomeOffice", viewModel ?? null);
}
public ActionResult SearchResults(HomeOfficeViewModel viewModel)
{
string field1 = TempData["Field1"].ToString();
if(viewModel == null)
viewModel = TempData["HomeOfficeViewModel1"] as HomeOfficeViewModel;
return View(viewModel);
}