2016-11-11 37 views
1

得到的數據我在MVC是很新:(MVC從充滿活力的領域

我通過克隆的主要DIV元素及其元素創建一個動態的形式要素是組合框,文本框和一個日期文本框。當我創建一個新的「克隆」,DIV的每個成員都有一個增量ID,如tab_Container,tab_Container_1,text,text1,combo,combo1等......現在,我試圖獲取Div中每個成員的值。到控制器

谷歌搜索,我覺得是這樣的:

[HttpPost] 
    public ActionResult NewEntry(Model Entry) 
    { 

     Control myControl = new Control(); 
     myControl.FindControl("Text0"); 

     if (myControl != null) 
     { 
     /// apparently, find the control,here i wanna to get the value of each field !! ¿? 
      /// do i have to create a list[] ... exist something like Entry.Text0 = myControl.value? 
     } 
     else 
     { 
      Response.Write("Control not found"); 
     } 

     return View(Entry); 
    } 

有什麼建議嗎?控制是最好的選擇?我必須在Javascript代碼中做其他事嗎?

+0

Control類是做什麼的?你知道ASP.NET MVC沒有像Web表單這樣的控件。禮拜? – Shyju

+0

什麼是「控制」?在這裏傳遞給方法的'Entry'模型中有什麼?什麼是實際的形式帖子? – David

+0

我嘗試使用Control.FinControl(方法)使用System.Web.UI.WebControls – Gleomarp

回答

0

儘管擁有某種Model/ViewModel通常會更好,但這種情況有點不同。 MVC綁定在表單輸入的「名稱」屬性上。

所以說,例如你的剃鬚刀語法生成這樣的事情:

<form> 
    <input type="text" name="input1" /> 
    <input type="text" name="input2" /> 
    <!-- etc etc etc --> 
    <input type="submit" value="submit" /> 
</form> 

,因爲這是動態生成的,你不必將乾淨綁定到該模型。您可以使用FormCollection類型作爲您的操作的參數。這會爲您提供發佈到服務器的所有項目的集合,然後您可以循環或同步以獲取所需的屬性。

public ActionResult myAction(FormCollection collection) 
{ 
    var value = collection["input1"]; 
    var value2 = collection["input2"]; 

    return View(); 
}