9

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") %>

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!

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

DOH! Rough day. Thanks for setting my head straight!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.