Vladimirs
Vladimirs

Reputation: 8599

Html.DropDownList inside Editor Template generates wrong name

I have following drop down declaration in my EditorTemplate in file named Category.cshtml

@Html.DropDownList("CategoryId", categoryList, new { style = "width: 100%;" })

And it appears that it generates Category.CategoryId instead of expected CategoryId which I specified as a name attribute value.

What I am doing wrong? Is it possible get the name exactly that what I specified using Html.DropDownList?

Update 1.

I am using that editor template in a Telerik grid context:

@(Html.Telerik().Grid<PriceCategoriesModel.PriceRuleModel>()
    .Name("pricecategories-grid")
    .DataKeys(keys => keys.Add(x => x.Id))
    .DataBinding(...)            
    .Columns(columns =>
        {
            columns.Bound(x => x.Vendor)
                    .Width(50);
            columns.Bound(x => x.Manufacturer)
                    .Width(100);
            columns.Bound(x => x.Category);
            //...
        }) //...)

Actually before I used Telerik's DropDown with the same name and name for select element was generated correctly.

Upvotes: 3

Views: 454

Answers (1)

Vladimirs
Vladimirs

Reputation: 8599

After decompiling Html.DropDownList I found that final field name calculated by prepending this.HtmlFieldPrefix + "." to specified name and trims "." after.

Then found how to set this prefix through ViewData.TemplateInfo.HtmlFieldPrefix.

Then I put things together I got this terrible solution.

@{
    var oldHtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    ViewData.TemplateInfo.HtmlFieldPrefix = String.Empty;
}

@Html.DropDownList("CategoryId", categoryList, new { style = "width: 100%;" })

@{
    ViewData.TemplateInfo.HtmlFieldPrefix = oldHtmlFieldPrefix;
}

Don't want to use that - please tell me that there is another fancy build-in way to handle that.

Upvotes: 2

Related Questions