使用 RedirectToAction 时隐藏路由值

发布于 2024-11-14 11:22:39 字数 1921 浏览 3 评论 0原文

    [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-11-21 11:22:39

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文