使用 RedirectToAction 时隐藏路由值
[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
我不应该在 url 中显示它们,因为我将发送 ssn。有没有办法隐藏它们或有更好的方法来做到这一点。
谢谢。
[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();
}
}
values in the viewModel are being shown in the url like this
http://sample.url.com/HomeOffice/SearchResults?FirstName=testing
I should not show them in the url because I will be sending ssn. Is there a way to hide them or any better way to do this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RedirectToAction
将创建一个对指定操作(在您的情况下为SearchResults
)的 GET 请求,该请求可能会尝试序列化您的视图模型字段。相反,您可以使用 TempDataRedirectToAction
will create a GET request to the named action (SearchResults
in your case) which is probably trying to serialize your view model fields. Instead, you could use TempData