开发者

ASP.NET MVC Model Binding a Collection With a Prefix

I want to bind a collection using a prefix, like so

public ActionResult Whatever([Bind(Prefix = "Prefix")] CustomModel[] models)
开发者_StackOverflow中文版

I created form elements using

<%= Html.TextBox("Prefix.models[" + i + "].Property") %>

which generated html inputs like this

<input id="Prefix_models[0]_Property" name="Prefix.models[0].Property" />

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.

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.

Please help. Cheers!


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) { ... }

The the binder expects foo[0], foo[1], etc.


UpdateModel() and TryUpdateModel() take a parameter for prefix. Have you tried that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜