2013-03-01 96 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 
    }