2014-10-22 56 views
-1

我有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); 
} 

請建議如何共同提交此表格。希望你能理解我的問題。 在此先感謝...

+0

你不能同時提交一個以上的形式..一次只能提交一份表格。 – 2014-10-22 06:31:02

+0

@Kartikeya ok然後我的問題有什麼解決辦法嗎? – 2014-10-22 06:31:43

+1

爲什麼不把它們全部包裝在一起? – Oliver 2014-10-22 06:32:43

回答

0

你應該使用JavaScript/jQuery的並在提交之前將隱藏輸入中以前表單中的所有值複製到最終表單中。

首先,你應該在你的最後一個表單添加隱藏的輸入是這樣的:

@using(Html.BeginForm("MyAction","MyController",FormMethod.Get,new{@id="form5"})) 
{ 
    for (int i = 1; i <= 8; i++) 
    { 
     <input type="hidden" name="[email protected](i)" id="[email protected](i)" /> 
     // - or - 
     // @Html.Hidden("hProp" + @i) 
    } 

@Html.TextBoxFor(m=>m.Prop9) 
@Html.TextBoxFor(m=>m.Prop10) 
<input type="submit" value="Save"/> 
} 

那麼這個jQuery代碼添加到您的網頁:

$("input[type='submit']").on("click", function() { 
    for (var i = 1; i <= 8; i++) { 
     $("#hProp" + i).val($("#Prop" + i).val()); 
    } 

}); 
+0

那麼如何將所有以前的表單值複製到最後一個表格中。 ById? – 2014-10-22 06:37:26

+0

請參閱最新的答案 – SmartDev 2014-10-22 06:58:43

+0

對了,讓我檢查.... – 2014-10-22 07:08:28

0

在最後一個表格中只有一個提交按鈕,它只會提交該表格中的控件。我建議使用一種形式,但將控件劃分爲多個部分(我假設您可能隱藏了以下部分,因此Next按鈕將驗證該部分中的控件,然後如果有效,則顯示下一部分。

對於部分中驗證控件可能看起來像代碼(我假設的控制後續組是隱藏的,如果當前部分是有效下一節纔可見)

$('button').click(function() { 
    var container = $(this).closest('fieldset'); // assumes groups of controlls and the button are wrapped in <fieldset> tag 
    var isValid = true; 
    $.each(container.find('input'), function() { // could add select, textarea if applicable 
    $('form').validate().element($(this)); 
    if (!$(this).valid()) { 
     isValid = false; 
     return false; 
    } 
    }); 
    if (isValid) { 
    container.next('fieldset').show().find('input').first().focus(); // set focus to first input of next fieldset 
    container.hide(); // hide the previous container? 
    } else { 
    // display an error message? 
    container.find('.error').text('please complete this section'); // assumes an element with class="error" to display message 
    } 
}); 
+0

是的,我不是intresting提交我的第一個4表格,這就是爲什麼我只是檢查他們的驗證,如果有效的顯示下一個表格。 我想知道如何合併前4種形式的值與最後一種形式。可能嗎 ? – 2014-10-22 06:36:34

+0

不,但按照我的建議做了什麼錯誤。在進入下一個控制之前,您可以只驗證以前的控制。 – 2014-10-22 06:38:07

+0

是的,但爲此,我需要檢查每個字段,然後再轉到下一個表單。這是一個虛擬的例子,我在模型中有90個字段。 – 2014-10-22 06:42:07

相關問題