Why is Html.DropDownList() producing <select name="original_name.name"> instead of just <select name

Posted by DanM on Stack Overflow See other posts from Stack Overflow or by DanM
Published on 2010-02-18T02:37:26Z Indexed on 2010/03/15 17:59 UTC
Read the original article Hit count: 596

I have an editor template whose job is to take a SelectList as its model and build a select element in html using the Html.DropDownList() helper extension.

I'm trying to assign the name attribute for the select based on a ModelMetadata property. (Reason: on post-back, I need to bind to an object that has a different property name for this item than the ViewModel used to populate the form.)

The problem I'm running into is that DropDownList() is appending the name I'm providing instead of replacing it, so I end up with names like categories.category instead of category.

Here is some code for you to look at...

SelectList.ascx

<%@ Control Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<System.Web.Mvc.SelectList>" %>
<%= Html.DropDownList(
    (string)ViewData.ModelMetadata.AdditionalValues["PropertyName"], Model) %>

Resulting HTML

<select id="SkillLevels_SkillLevel" name="SkillLevels.SkillLevel">
    <option value="1">High</option>
    <option value="2">Med</option>
    <option selected="selected" value="3">Low</option>
</select>

Expected HTML

<select id="SkillLevels_SkillLevel" name="SkillLevel">
    <option value="1">High</option>
    <option value="2">Med</option>
    <option selected="selected" value="3">Low</option>
</select>

Also tried

<%= Html.Encode((string)ViewData.ModelMetadata.AdditionalValues["PropertyName"])%>

...which resulted in "SkillLevel" (not "SkillLevels.SkillLevel"), proving that the data stored in metadata is correct.

and

<%= Html.DropDownList(
    (string)ViewData.ModelMetadata.AdditionalValues["PropertyName"], Model,
    new { name = (string)ViewData.ModelMetadata.AdditionalValues["PropertyName"] }) %>

...which still resulted in <select name=SkillLevels.Skilllevel>.

Questions

What's going on here? Why does it append the name instead of just using it? Can you suggest a good workaround?

Update:

I ended up writing a helper extension that literally does a find/replace on the html text:

public static MvcHtmlString BindableDropDownListForModel(this HtmlHelper helper)
{
    var propertyName = (string)helper.ViewData.ModelMetadata.AdditionalValues["PropertyName"];
    var compositeName = helper.ViewData.ModelMetadata.PropertyName + "." + propertyName;
    var rawHtml = helper.DropDownList(propertyName, (SelectList)helper.ViewData.Model);
    var bindableHtml = rawHtml.ToString().Replace(compositeName, propertyName);
    return MvcHtmlString.Create(bindableHtml);
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2