开发者

Hide Route values when using 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();
        }
 }

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.


RedirectToAction 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

[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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜