I adapted this code Has anyone implement RadioButtonListFor<T> for ASP.NET MVC? for combobox.
But I’ve a small problem is some case(which appears with checkbox/radiobutton:
If The item I want to bind is in a list, I get weird names:
//Here I've some for loop
@Html.CheckBoxListFor(m => m.Variables[i].Values[j].Triggers)
And I got:
<input id="Variables.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+<>c__DisplayClass2).i).Values.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+<>c__DisplayClass4).j).Triggers_103" name="Variables.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+<>c__DisplayClass2).i).Values.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+<>c__DisplayClass4).j).Triggers.ListItems[0].Selected" type="checkbox" value="true" />
How to avoid this name?
EDIT:
The list of item is dynamic and has to be loaded from the controller:
pulic ActionResult DoMyThing(){
_dataStore.MakeThings();
....
MyModel model = new MyModel{SelectedId=5, AvailableObjects= _aPreviouslyLoadedList};
return View(model);
}
I just discovered a VERY good solution (for my needs) regarding checkboxes, dropdownlists, radiobuttons, etc.
Check out this post (link on bottom to code on codeplex -> source -> download latest):
Adding Rich Selector Support for MVC by Matt Hidinger
Note that the post indicates you need three (3) extension files to get it to work, but there are also some other helper extensions that are needed (DelimitedStringHelper.cs, HtmlFormHelper.cs, ReflectionHelper.cs and StringHelper.cs are the other ones you need).