我有1个视图模型。 而对于一些用户的需求和要求,我把这个模型分成5种形式,这里就是例子。合并多个子表单并在Asp.net MVC4中作为单一表单提交
public class VM_MyModel
{
public string Prop1 {get;set}
public string Prop2 {get;set}
public string Prop3 {get;set}
public string Prop4 {get;set}
public string Prop5 {get;set}
public string Prop6 {get;set}
public string Prop7 {get;set}
public string Prop8 {get;set}
public string Prop9 {get;set}
public string Prop10 {get;set}
}
在视图上有5个表单相应地包含模型属性。
@using(Html.BeginForm("","",FormMethod.Post,new{@id="form1"}))
{
@Html.TextBoxFor(m=>m.Prop1)
@Html.TextBoxFor(m=>m.Prop2)
<input type="button" value="Next"/>
}
@using(Html.BeginForm("","",FormMethod.Post,new{@id="form2"}))
{
@Html.TextBoxFor(m=>m.Prop3)
@Html.TextBoxFor(m=>m.Prop4)
<input type="button" value="Next"/>
}
@using(Html.BeginForm("","",FormMethod.Post,new{@id="form3"}))
{
@Html.TextBoxFor(m=>m.Prop5)
@Html.TextBoxFor(m=>m.Prop6)
<input type="button" value="Next"/>
}
@using(Html.BeginForm("","",FormMethod.Post,new{@id="form4"}))
{
@Html.TextBoxFor(m=>m.Prop7)
@Html.TextBoxFor(m=>m.Prop8)
<input type="button" value="Next"/>
}
@using(Html.BeginForm("MyAction","MyController",FormMethod.Post,new{@id="form5"}))
{
@Html.TextBoxFor(m=>m.Prop9)
@Html.TextBoxFor(m=>m.Prop10)
<input type="submit" value="Save"/>
}
这里所有的前4种形式我改变默认行为,并调用jQuery的.valid()函数来检查渡过形式是否有效,如果无效,则打开窗口2并以此类推,直到那个达到form5表单我想在我的控制器动作中共同提交我的所有5个表单,这些动作接收VM_MyModel作为参数。这里代码
[HttpPost]
public ActionResult MyAction(VM_MyModel model)
{
if(ModelState.IsValid)
{
model.Save();
RedirectToAction("Home");
}
return View(model);
}
请建议如何共同提交此表格。希望你能理解我的问题。 在此先感谢...
你不能同时提交一个以上的形式..一次只能提交一份表格。 – 2014-10-22 06:31:02
@Kartikeya ok然后我的问题有什么解决办法吗? – 2014-10-22 06:31:43
为什么不把它们全部包装在一起? – Oliver 2014-10-22 06:32:43