2013-03-01 118 views
0

我正在使用MVC。我有一個觀點說,廠景下面給出其中包括一個HTML表單:將參數從HTML頁面傳遞給控制器​​

<form action='/Exercise/Exercise4_SubmitForm' method="post"> 
    Name: <input type="text" name="name" /><br /> 
    Emp: <input type="text" name="id" /><br /> 
    DateofBirth: <input type="text" name="Dateofbirth" /><br /> 
    Age: <input type="text" name="age" /><br /> 
    . 
    so on 
    . 
    . 
    <input type="submit" name="submit" value="Save" /> 
    </form> 

我從我剛纔所說的只有4個以上17個文本框。

現在MVC操作方法必須是

public ActionResult Exercise4(string name, string code, string age, string dateofBirth,...) 
    { 
     //my code here 
    } 

我的問題是, 有由我不具有與上述在我的MVC操作方法指定每個17個參數的任何方式。因爲下次可能有70個參數,那麼指定每個參數是非常繁瑣的工作。 「我不想使用HTML助手。」

請幫幫我!!!

+2

試圖做這種方式被完全忽視的力量的作用mvc框架。谷歌的''asp.net mvc模型綁定'或者花費幾個小時的時間來完成衆多在線教程中的一個。如果您將在此框架中開發,您必須瞭解這一點。 – 2013-03-01 13:02:56

回答

2

創建模型

public class YourModel 
{ 
    public string name {get;set;} 
    public string code {get;set;} 
    public string age {get;set;} 
    public string date {get;set;} 
    . 
    . 
    . 
} 

然後,在視圖的頂部放

@model Namespace.YourModel 

之後切換所有的輸入,

@Html.EditorFor(m => m.name) 
@Html.EditorFor(m => m.code) 
@Html.EditorFor(m => m.age) 
@Html.EditorFor(m => m.date) 

如果您不確定如何做最後一部分,創建一個新的視圖,並檢查創建一個強類型的視圖,並選擇你的模型obox。 根據您的需要選擇一個腳手架模板。那是一個形式插入,修改,刪除,列出....

接收後會收到你的模型,而不是

public ActionResult Exercise4(YourModel model) 
    { 
     //my code here 
    } 
相關問題