开发者

specify name of dropdownlist asp.net mvc3 razor

I am wondering why the following code:

@Html.DropDownList("Classification.Nationality.NationalityId", Model.Nationalities, new { @size = 10, @style = "display:none;", @class = "pickList" })

produces the following html, specifically why the name of the element is not "Classification.Nationality.NationalityId".

<select style="display: none;" size="10" name="CollectionCategory.Classification.Nationality.NationalityId" id="CollectionCategory_Classification_Nationality_NationalityId" class="pickList">
开发者_Go百科

where the function signature sure looks like this:

public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes);

It seems like the name parameter gots overriden by view model of the parent view. ( This is in a partial view). Does this make sense to anyone?


It's because you are calling this helper inside an editor template or partial for a navigational property called CollectionCategory. It's perfectly normal behavior and ensures that proper value is sent to the controller action when binding. Also I would recommend you using the strongly typed version of this helper to avoid those refactor unfriendly magic strings:

@Html.DropDownListFor(
    x => x.Classification.Nationality.NationalityId, 
    Model.Nationalities, 
    new { 
        @size = 10, 
        @style = "display:none;", 
        @class = "pickList" 
    }
)

Of course if you don't want to follow conventions (no idea why wouldn't you) but you could specify a binding prefix in your POST action:

[HttpPost]
public ActionResult Foo([Bind(Prefix = "CollectionCategory")] ClassificationViewModel model)
{
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜