表单验证失败时,将错过查询字符串参数

时间:2013-11-05 12:39:35

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

我有一个包含以下网址的表单: 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);
        }

EDIT。 我的观点:

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">

3 个答案:

答案 0 :(得分:2)

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

[HttpGet]
public ActionResult CreateEntity(int? officeCodeId)

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

答案 1 :(得分:1)

您的行为应如下所示:

操作:

[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表单上,您可以将其存储在隐藏字段中。

答案 2 :(得分:0)

您的最终答案很好,而且效果很好,尽管您可以通过简单地添加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);
    }