2

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+&lt;>c__DisplayClass2).i).Values.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+&lt;>c__DisplayClass4).j).Triggers_103" name="Variables.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+&lt;>c__DisplayClass2).i).Values.get_Item(value(ASP._Page_Areas_Advertiser_Views_Campaign_ValueDependency_cshtml+&lt;>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);
}
Community
  • 1
  • 1
J4N
  • 19,480
  • 39
  • 187
  • 340

2 Answers2

1

Can you post your extension method for CheckBoxListFor()? Depending on your possible parameters, if you have an object parameter for htmlAttributes, you could do something like:

new { id = "yourDesiredIdName" }
  • It's the same than the code of my link(the problem is the same with the RadioButtonListFor – J4N Feb 13 '12 at 19:37
0

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

REMESQ
  • 1,190
  • 2
  • 26
  • 60
  • I checked your code, it seems good, but, in my case, elements are coming from the controller, it seems that with your code, I can't specify from what it comes, I have to specify it through an attribute. – J4N Feb 13 '12 at 19:44
  • @J4N what do you mean elements are coming from your controller? Maybe post a snippet in your question? Also, I note that you "adapted" code from another post. This is not a bad thing (SO is a great resource for things like that). IMHO, I spent a lot of time cobbling pieces of code together (radiobuttonlists, checkboxlists, dropdownlists) and found that getting them to follow conventions I was used to (model, viewmodel, controller, view, editortemplates) was impossible because they all took different approaches. The code above alleviated that headache for me. – REMESQ Feb 13 '12 at 20:15
  • Thank you for your response, I added a small snippet to explain what I was meaning by "elements are coming from the controller" – J4N Feb 14 '12 at 05:00