开发者

Model Binding to Multiple Types with Same Property Names

I have a view that contains 2 list boxes: Audience & Locale

I'm trying to submit a form that contains the selected index of both to my controller, which has an action method of this signature:

public JsonResult Submit(Audience aud, Locale loc)
{
// do stuff
}

The problem I'm having is both Audience & Locale have an ID property, so the Model Binder is a little bit confused over which to assign the selected values to. Both get assigned the value '1', when Audience should have '2' and Locale should have '1'

The question is how can I get the page to differentiate between the two when it's submitting? We've tried prepending the ID value for Locale with "locale.", so the parameter string that gets passed开发者_开发知识库 as data to the Controller looks like "&locale.id=1&audience.id=2" but that doesn't seem to work.

Any suggestions? If more info is needed, I'll supply it.

Thanks

Dave


Use:

public JsonResult Submit([Bind(Prefix = "audience")]Audience aud,[Bind(Prefix = "locale")]Locale loc)
{
// do stuff
}

Every Audience field should have "audience" prefix in name in html field: audience.id,audience.name,...

<input id="audience_name" name="audience.Name" type="text" value="" />


You should have a specific ViewModel for taking data into your Submit Action. That object should have AudienceId and LocaleId. When you create your dropdowns you should create them with names that match what you expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜