我有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