【问题标题】:Query string param is missed when form validation fail表单验证失败时缺少查询字符串参数
【发布时间】:2018-07-05 22:44:06
【问题描述】:

我有一个带有以下网址的表单: CreateEntity?officeCodeId=5

当我发送表单进行验证时,如果验证失败,它只返回 CreateEntity url。没有 officeCodeId=5。

如果用户在 URL 或 F5 上单击输入 - 我的站点失败 - 它需要缺少 officecodeId 参数。我可以将它保存到会话或其他存储中。但我想把它放在 URL 里

我的看法:

[HttpGet]
        public virtual ActionResult CreateEntity(int? officeCodeId)
        {            
            var model = new CreateViewModel();
            FillViewModel(model, officeCodeId);
            return View("Create", model);
        }


[HttpPost]
protected virtual ActionResult CreateEntity(TEditViewModel model)
        {
            if (ModelState.IsValid)
            {
              //Do some model stuff if 
            }

            return View("Create", model);
        }

编辑。 我的观点:

using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(x => x.OfficeCodeId)  
<div>
                @Html.LabelFor(model => model.FirstName, CommonRes.FirstNameCol)
                @Html.TextBoxFor(model => model.FirstName, Model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
            <div>
                @Html.LabelFor(model => model.LastName, CommonRes.LastNameCol)
                @Html.TextBoxFor(model => model.LastName, Model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
<div> <div class="input-file-area"></div>
                    <input id="Agreements" type="file" name="Agreements"/>
</div>   

}

编辑 2。 添加:

@using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { officeCodeId = Model.OfficeCodeId, enctype = "multipart/form-data" }))

没有帮助。 它产生以下形式:

<form action="/PhoneEmployee/CreateEntity" enctype="multipart/form-data" method="post" officecodeid="5">

解决方案是

<form action="@Url.Action("CreateEntity", "Employee")?officecodeid=@Model.OfficeCodeId" enctype="multipart/form-data" method="post">

【问题讨论】:

  • 问题是什么?
  • 如果验证失败,如何恢复查询字符串值?officeCodeId=5。
  • 优秀的解决方案!

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

问题是您的 HttpPost 操作没有任何 id 参数的概念。如果您想支持类似的 URL,则使操作签名支持该参数,例如

[HttpGet]
public ActionResult CreateEntity(int? officeCodeId)

[HttpPost]
public ActionResult CreateEntity(int officeCodeId, EditViewModel model);

【讨论】:

  • 它没有帮助。验证失败后,MVC 仍然剪切查询字符串参数。
  • @IvanKorytin 并且您正在发布到相同的 URL,例如CreateEntity?officeCodeId=5?您能否展示一下您在视图中的发布方式?
  • 谢谢。我添加了表单。
  • @IvanKorytin 问题是您的 id 当前是您的 POST 正文的一部分,因此它不会对 URL 产生任何影响,如果您将表单代码更改为类似于 using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { officeCodeId = "@Model.OfficeId", enctype = "multipart/form-data" })) 它应该工作。
  • 你是对的。你可以看到解决方案对我有用。
【解决方案2】:

您的操作应如下所示:

动作:

[HttpGet]
public virtual ActionResult CreateEntity(int? officeCodeId)
{            
    var model = new CreateViewModel();
    FillViewModel(model, officeCodeId);
    return View("Create", model);
}

[HttpPost]
public virtual ActionResult CreateEntity(ViewModel model)
{            
    if (model.IsValid) {
       // save...
       return RedirectToAction("EditEntity", newId!!!);
    } 

    return View("Create", model);
}

HTML:

 @using (Html.BeginForm()) {
     @Html.HiddenFieldFor(m => Model.officeCodeId)
     ...
 } 

您的 officeId 应该在模型中。在 html 表单上,您可以将其存储在隐藏字段中。

【讨论】:

  • 是笔误。问题是一样的。
  • 作者更改了描述。我已经回答了最初的描述。
  • 是的,我做到了。名称转换对我的问题并不重要。
  • 检查问题中的html部分。
  • "您的 officeId 应该在模型中"。它已经在那里了。 url中的问题不在模型中。
【解决方案3】:

您的最终答案非常好并且效果很好,尽管您可以通过简单地添加 Request.QueryString 来进一步增强它以使其更通用:

<form action="@Url.Action("CreateEntity", "Employee")?@(Request.QueryString)"
  enctype="multipart/form-data" method="POST">

然后使用POST 操作:

[HttpPost]
protected virtual ActionResult CreateEntity(TEditViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多