ASP.NET MVC模型绑定一个集合与preFIX [英] ASP.NET MVC Model Binding a Collection With a Prefix
问题描述
我想用一个preFIX绑定的集合,像这样
I want to bind a collection using a prefix, like so
public ActionResult Whatever([Bind(Prefix = "Prefix")] CustomModel[] models)
我使用创建的窗体元素
I created form elements using
<%= Html.TextBox("Prefix.models[" + i + "].Property") %>
而生成的html输入这样的
which generated html inputs like this
<input id="Prefix_models[0]_Property" name="Prefix.models[0].Property" />
我的问题是,默认模式粘结剂将无法与preFIX绑定。我得到空在操作方法精氨酸的车型。
My problem is that the default model binder will not bind with a prefix. I get null for the models arg in the action method.
如果我剥从HTML的prefixes并删除绑定属性,一切工作正常。我无法想象,默认模式粘结剂不会处理一个集合preFIX,所以我必须做一些错误的。
If I strip the prefixes from the html and remove the Bind attribute, everything works fine. I cannot imagine that the default model binder won't handle a prefix on a collection, so I must be doing something wrong.
请帮忙。干杯!
推荐答案
的 preFIX 的[绑定]里面是不是prepended参数名,它会替换参数名称完全。所以,如果你的操作方法有此签名:
Prefix inside of [Bind] isn't prepended to the parameter name, it replaces the parameter name entirely. So if your action method has this signature:
public ActionResult MyAction([Bind(Prefix = "foo")] string[] bar) { ... }
该粘结剂预计 foo的[0]
, foo的[1]
等
这篇关于ASP.NET MVC模型绑定一个集合与preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!