当表单验证失败的查询字符串参数是错过了 [英] Query string param is missed when form validation fail
问题描述
我有以下URL的形式:
CreateEntity?办公codeID = 5
当我发送的形式来验证,如果验证失败是它返回只是CreateEntity网址。没有办公室codeID = 5。
如果用户点击URL或F5输入 - 我的地盘失败 - 它需要缺少办公codeID参数。我可以将它保存到会话或其他存储。但我想有它的网址
我的观点:
[HTTPGET]
公共虚拟的ActionResult CreateEntity(INT?办公室codeID)
{
VAR模型=新CreateViewModel();
FillViewModel(型号,办公室codeID);
返回视图(创建,模型);
}
[HttpPost]
受保护的虚拟的ActionResult CreateEntity(TEditViewModel模型)
{
如果(ModelState.IsValid)
{
//做一些模型的东西,如果
} 返回视图(创建,模型);
}
编辑。
我的观点:
使用(Html.BeginForm(CreateEntity,雇员,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
@ Html.HiddenFor(X => x.Office codeID)
< DIV>
@ Html.LabelFor(型号=> model.FirstName,CommonRes.FirstNameCol)
@ Html.TextBoxFor(型号=> model.FirstName,Model.FirstName)
@ Html.ValidationMessageFor(型号=> model.FirstName)
< / DIV>
< DIV>
@ Html.LabelFor(型号=> model.LastName,CommonRes.LastNameCol)
@ Html.TextBoxFor(型号=> model.LastName,Model.LastName)
@ Html.ValidationMessageFor(型号=> model.LastName)
< / DIV>
< DIV> < DIV CLASS =输入文件名区>< / DIV>
<输入ID =协议类型=文件名称=协议/>
< / DIV>
}
编辑2。
添加:
@using(Html.BeginForm(CreateEntity,雇员,FormMethod.Post,新的办公{codeID = Model.Office codeID,ENCTYPE =的multipart / form-data的}))
Haven`t帮助。
它产生以下形式:
<形式的行动=/ PhoneEmployee / CreateEntityENCTYPE =的multipart / form-data的方法=邮报的办公室codeID =5>
的解决方案是
<形式的行动=@ Url.Action(CreateEntity,员工)?office$c$cid=@Model.Office$c$cId~~VENCTYPE =的multipart / form-data的方法=后>
问题是你的 HttpPost
动作没有的任何观念 ID
参数。如果你想支持一个类似的URL,然后做出行动签名支持该参数例如
[HTTPGET]
公众的ActionResult CreateEntity(INT?办公室codeID)[HttpPost]
公众的ActionResult CreateEntity(INT办公室codeID,EditViewModel模型);
I have an form with following url: CreateEntity?officeCodeId=5
When I send form to validate and if validation is fail it returns just CreateEntity url. No officeCodeId=5.
if user click enter on URL or F5 - my site fail - it require missing officecodeId param. I can save it to the session or in the other storage. But I want to have it in the URL
My view:
[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. My View:
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>
}
Edit 2. Adding:
@using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { officeCodeId = Model.OfficeCodeId, enctype = "multipart/form-data" }))
Haven`t help. It produce the following form:
<form action="/PhoneEmployee/CreateEntity" enctype="multipart/form-data" method="post" officecodeid="5">
Solution Is
<form action="@Url.Action("CreateEntity", "Employee")?officecodeid=@Model.OfficeCodeId" enctype="multipart/form-data" method="post">
The problem is your HttpPost
action doesn't have any notion of an id
parameter. If you want to support a similar URL then make the action signature support that parameter e.g.
[HttpGet]
public ActionResult CreateEntity(int? officeCodeId)
[HttpPost]
public ActionResult CreateEntity(int officeCodeId, EditViewModel model);
这篇关于当表单验证失败的查询字符串参数是错过了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!