使用模型重定向到响应视图不会保留模型属性

问题描述 投票:0回答:1

我有一个表单视图,它将表单数据提交到控制器上的post操作,然后重定向到另一个使用逻辑显示成功或失败的视图,但新视图只显示模型属性的空白值。这是后期行动:

[HttpPost]
    public ActionResult ContactUs(TTT.Models.ContactUsModel model)
    {
        logger.Info(model.URL + "Contact Us Form submitted");

        var userkey = model.ValidationKey;
        var sessionkey = Session["ContactUsKey"];
        var lastsubmission = Session["ContactUsTime"];
        model.Response = "success";

        //first check if honeypot was populated via a bot and if so send it to the success page without doing anything
        if (model.WorkAddress != "")
        {
            logger.Info("honeypot triggered");
            return View("ContactUsResponse", model);
        }

我会遗漏控制器的其余部分,但是

以下是它重定向到的视图:

@using TTT.Models
@using Sitecore.Mvc
@model ContactUsModel


 <h1>@Model.Title</h1>
 <div>@Model.Body</div>

  <div>
     @if (@Model.Response == "fail")
      {
          @Model.Failure;
      } else
      {
          @Model.Success;
      }
   </div>
asp.net-mvc-4 sitecore8
1个回答
0
投票

不要返回新视图,而是调用RedirectToAction并从该控制器返回新视图。

[HttpPost]
public ActionResult ContactUs(TTT.Models.ContactUsModel model)
{
    //--- Code omitted for brevity

    if (model.WorkAddress != "")
    {
        logger.Info("honeypot triggered");
        return RedirectToAction("ContactUsResponse", new { response = model });
    }
}

public ActionResult ContactUsResponse(TTT.Models.ContactUsModel response) 
{
    return View(model)
}
© www.soinside.com 2019 - 2024. All rights reserved.