我使用这个编辑器模板来使用相同属性名的ViewBag/ViewData的Dropdownlist列表
@model System.String
@*
For Using this Editor Template
- There should be a viewbag/viewdata (type SelectList) of same name as of calling Model's Property
*@
@{
var modelMetadata = ViewData.ModelMetadata;
// Get property name of the model
var propertyname = modelMetadata.PropertyName;
}
@if (ViewData[propertyname] == null)
{
@Html.DropDownList(propertyname , Enumerable.Empty<SelectListItem>(), "--Select--", new { @class = "form-control" })
}
else
{
@Html.DropDownList(propertyname , null, "--Select--", new { @class = "form-control" })
}现在使用它作为@Html.EditorFor(i=>i.Country,"CustomDropDown"),我也有一个ViewBag.Country作为国家的SelectList。
一切都正常,但是现在控件的命名变成了
<select class="form-control" id="Country_Country" name="Country.Country">如何从id和名称中删除附加的Country?
附加信息:
我本可以只使用@Html.DropDownList("Country"),但它不允许我将css类添加到控件中。
发布于 2014-05-30 06:51:52
我想我找到了解决办法。
我将DropDownList更改为DropDownListFor,并作了一些更改
@if (ViewData[propertyname] == null)
{
@Html.DropDownListFor(m=>m, Enumerable.Empty<SelectListItem>(), "--Select--", new { @class = "form-control" })
}
else
{
@Html.DropDownListFor(m => m, ViewData[propertyname] as SelectList, "--Select--", new { @class = "form-control" })
}这个自动ViewData绑定有时有点混乱。>_<
https://stackoverflow.com/questions/23948549
复制相似问题