C# 如何检索MVC页面上所有输入的所有值
我有一个MVC3项目,我正在启动,使用c#和Razor。我有一个页面,大约有20个输入字段将被使用。我创建ViewModel以将数据传递给视图以创建页面。当用户提交表单时,我对如何获取字段值感到困惑C# 如何检索MVC页面上所有输入的所有值,c#,asp.net-mvc,asp.net-mvc-3,model-view-controller,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,Razor,我有一个MVC3项目,我正在启动,使用c#和Razor。我有一个页面,大约有20个输入字段将被使用。我创建ViewModel以将数据传递给视图以创建页面。当用户提交表单时,我对如何获取字段值感到困惑 我的控制器是否必须为页面上的每个输入字段设置一个参数?有没有办法让控制器获取页面上的所有数据,然后我可以通过它进行解析?参数列表将非常庞大。您可以使用在后续操作中传递给视图的模型作为参数 例如: //This is your initial HTTP GET request. public Acti
我的控制器是否必须为页面上的每个输入字段设置一个参数?有没有办法让控制器获取页面上的所有数据,然后我可以通过它进行解析?参数列表将非常庞大。您可以使用在后续操作中传递给视图的模型作为参数 例如:
//This is your initial HTTP GET request.
public ActionResult SomeAction() {
MyViewModel model;
model = new MyViewModel();
//Populate the good stuff here.
return View(model);
}
//Here is your HTTP POST request; notice both actions use the same model.
[HttpPost]
public ActionResult SomeAction(MyViewModel model) {
//Do something with the data in the model object.
}
第二个方法中的模型对象将根据HTTP请求中包含的数据自动填充(技术名称为“模型绑定”)。您可以使用传递给视图的同一模型作为后续操作中的参数 例如:
//This is your initial HTTP GET request.
public ActionResult SomeAction() {
MyViewModel model;
model = new MyViewModel();
//Populate the good stuff here.
return View(model);
}
//Here is your HTTP POST request; notice both actions use the same model.
[HttpPost]
public ActionResult SomeAction(MyViewModel model) {
//Do something with the data in the model object.
}
第二种方法中的模型对象将根据HTTP请求中包含的数据自动填充(技术名称为“模型绑定”)。请在控制器中以模型为参数创建mvc操作
Like this:
[HttpPost] or [HttpGet]
public ActionResult Employee(EmployeeModel employee)
{
// now you will have all the input inside you model properties
//Model binding is doen autoamtically for you
}
请在控制器中以模型为参数创建mvc操作
Like this:
[HttpPost] or [HttpGet]
public ActionResult Employee(EmployeeModel employee)
{
// now you will have all the input inside you model properties
//Model binding is doen autoamtically for you
}
在控制器的操作中,期望收到与传递回视图的相同的“模型”。如果您正确地生成了“输入控件”(通过使用
Html.TextBoxFor()
或通过将Name
属性设置为模型属性的相同名称),则这将起作用
注:MVC将使用ModelBinder,根据用户提交的字段确定如何创建和填充预期操作的对象属性
如果要捕获用户的所有输入,可以执行操作以接收类型为FormCollection
的对象:
public ActionResult MyAction(FormCollection values)
{
...
}
在控制器的操作中,期望收到与传递回视图的相同的“模型”。如果您正确地生成了“输入控件”(通过使用
Html.TextBoxFor()
或通过将Name
属性设置为模型属性的相同名称),则这将起作用
注:MVC将使用ModelBinder,根据用户提交的字段确定如何创建和填充预期操作的对象属性
如果要捕获用户的所有输入,可以执行操作以接收类型为FormCollection
的对象:
public ActionResult MyAction(FormCollection values)
{
...
}