asp.net-mvc 如何从 MVC 3 中的视图将列表或集合返回到控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9218602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I return List or Collection to Controller from View in MVC 3?
提问by ob1Jakobi
Someone please help me return this list properly from my view. I don't see why I'm returning null for my fieldModelList I try to pass to the controller...
有人请帮助我从我的角度正确返回此列表。我不明白为什么我要为我的 fieldModelList 返回 null 我试图传递给控制器......
Here is my view:
这是我的观点:
@model List<Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel>
<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.11.min.js"></script>
@using (Html.BeginForm("GetResponse", "TestMethods", FormMethod.Post))
{
<table id="tblMethods">
<tr>
<th>
Property Name
</th>
<th>
Request
</th>
</tr>
@foreach (FieldModel fieldModel in Model)
{
<tr>
<td>
@Html.DisplayFor(m => fieldModel.PropertyName)
</td>
<td>
@Html.TextBoxFor(m => fieldModel.PropertyValue)
</td>
</tr>
}
</table>
<div>
<input type="submit"/>
</div>
and here is my controller:
这是我的控制器:
[HttpPost]
public ActionResult GetResponse(List<FieldModel> fieldModelList)
{
return GetResponse(fieldModelList);
}
I am hitting the HttpPost method but if I place a breakpoint just inside it, I am returning null for the fieldModelList right off the bat, which I was hoping would be a list of the values I entered into the texboxes on the view that is of model FieldModel...
我正在使用 HttpPost 方法,但是如果我在其中放置一个断点,我会立即为 fieldModelList 返回 null,我希望这将是我在视图中输入到 texboxes 的值的列表模型字段模型...
I think something is wrong with my logic versus my syntax, or as maybe as well as my syntax, but basically what I want to do is return back a list of type FieldModel with each corresponding PropertyName and PropertyValue to the controller. I noticed I am not passing any kind of id parameter in my BeginForm statement in the view. Do I need one here?
我认为我的逻辑与语法有问题,或者可能和我的语法一样,但基本上我想要做的是将带有每个对应 PropertyName 和 PropertyValue 的 FieldModel 类型列表返回给控制器。我注意到我没有在视图中的 BeginForm 语句中传递任何类型的 id 参数。我这里需要吗?
Just in case, here is my model class for FieldModel:
以防万一,这是我的 FieldModel 模型类:
namespace Regions.SOA.UI.CopyBookSchemaCreator.Models
{
public class FieldModel
{
[Display(Name = "Property")]
public string PropertyName { get; set; }
[Display(Name = "Value")]
public string PropertyValue { get; set; }
}
}
采纳答案by David Fox
Phil Haack wrote an articlesome time ago explaining how to bind collections (ICollection) to view models. It goes into additional detail about creating an editor template, which you could certainly do as well.
Phil Haack前段时间写了一篇文章,解释了如何将集合 ( ICollection)绑定到视图模型。它详细介绍了有关创建编辑器模板的详细信息,您当然也可以这样做。
Basically, you need to prefix the HTML elements' name attributes with an index.
基本上,您需要在 HTML 元素的名称属性前加上索引。
<input type="text" name="[0].PropertyName" value="Curious George" />
<input type="text" name="[0].PropertyValue" value="H.A. Rey" />
<input type="text" name="[1].PropertyName" value="Ender's Game" />
<input type="text" name="[1].PropertyValue" value="Orson Scott Card" />
Then, your controller could bind the collection of FieldModel
然后,您的控制器可以绑定 FieldModel
[HttpPost]
public ActionResult GetResponse(List<FieldModel> fieldModelList)
{
return GetResponse(fieldModelList);
}
I'm not 100% sure the following would name the attributes correctly (I'd recommend using the editor template) but you could easily use the htmlAttributesargument and give it a name using the index.
我不是 100% 确定以下内容会正确命名属性(我建议使用编辑器模板),但您可以轻松使用该htmlAttributes参数并使用index.html 为其命名。
@for(int i = 0;i < Model.Count;i++)
{
<tr>
<td>
@Html.DisplayFor(m => m[i].PropertyName)
</td>
<td>
@Html.TextBoxFor(m => m[i].PropertyValue)
</td>
</tr>
}
Editor Template
编辑器模板
If you wanted to go as far as adding an editor template, add a partial view named FieldModel.ascxto /Views/Sharedthat is strongly typedto a FieldModel
如果你想尽可能增加一个编辑模板去,添加一个名为的局部视图FieldModel.ascx,以/Views/Shared该是强类型的FieldModel
@model Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel
@Html.TextBoxFor(m => m.PropertyName) @* This might be a label? *@
@Html.TextBoxFor(m => m.PropertyValue)
And, then the part of your view responsible for rendering the collection would look like:
然后,负责呈现集合的视图部分将如下所示:
@for (int i = 0; i < Model.Count; i++) {
@Html.EditorFor(m => m[i]);
}

