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

时间:2019-02-12 00:33:56

标签: asp.net-mvc-4 sitecore8

我有一个表单视图,该表单视图将表单数据提交到控制器上的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>

1 个答案:

答案 0 :(得分: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)
}