Question

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

Was it helpful?

Solution

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="" />

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top