我正在处理MVC4,using @Html.EditorForModel(),它将dropdownlist显示为文本框,我希望通过设置任何attribute和重写模板来显示Dropdown列表。请与我分享MVC4的例子。
@model Models.Employee
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}提前谢谢。
发布于 2013-07-20 08:59:03
您可以定义表示下拉列表的视图模型:
public class ItemsViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}它将用于您的主视图模型中:
public class MyViewModel
{
public ItemsViewModel DropDown1 { get; set; }
public ItemsViewModel DropDown2 { get; set; }
...
}现在剩下的就是为ItemsViewModel编写一个自定义编辑器模板,这个模板应该放在~/Views/Shared/EditorTemplates/ItemsViewModel.cshtml中。请注意,模板的名称和位置非常重要:
@model ItemViewModel
@Html.DropDownListFor(x => x.SelectedValue, Model.Values)这差不多就是全部了。
发布于 2013-07-20 08:27:37
它是最重要的模板吗?您可以使用@Html.DropDownList()助手。
有一个关于如何在asp.net网站中使用的完整示例,您可以下载该项目:这里
https://stackoverflow.com/questions/17760271
复制相似问题