IDI am using the following code to create a drop down list:
@for (var index = 0; index < Model.AdminSummaries.Count(); index++)
{
&
My solution to this issue is to replace the id and name after DropDownListFor() with a html helper extension method.
public static MvcHtmlString ReplaceIdAndName(this MvcHtmlString htmlSource, string name)
{
MvcHtmlString result;
if (name == null)
result = htmlSource;
else
{
string id = name.Replace("[", "_").Replace("]", "_").Replace(".", "_"); // HTML ids cannot have []. so MVC convention replaces with _
XDocument html = XDocument.Parse(htmlSource.ToHtmlString());
html.Elements().Select(e => e.Attribute("id")).FirstOrDefault().SetValue(id);
html.Elements().Select(e => e.Attribute("name")).FirstOrDefault().SetValue(name);
result = MvcHtmlString.Create(html.ToString());
}
return result;
}
DropDownListFor(...).ReplaceIdAndName("myclass[1].myprop");