@ Html.EditorForModel()下拉 [英] @Html.EditorForModel() Dropdown
问题描述
我工作的 MVC4 ,使用@ Html.EditorForModel()
,它显示的DropDownList
作为一个文本框,我想通过设置任何属性
和压倒一切的模板显示下拉
列表。请分享我的 MVC4 示例这一点。
I am working on MVC4 , using @Html.EditorForModel()
, it is showing dropdownlist
as a text box, I want to show Dropdown
list by setting any attribute
and overriding template. Please share me MVC4 example for this.
@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")
}
先谢谢了。
推荐答案
您可以定义将重新present下拉视图模型:
You could define a view model that will represent a dropdown:
public class ItemsViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
这将在主视图模型被使用:
which will be used in your main view model:
public class MyViewModel
{
public ItemsViewModel DropDown1 { get; set; }
public ItemsViewModel DropDown2 { get; set; }
...
}
现在所有剩下的就是写一个自定义编辑器模板的 ItemsViewModel
这应该放在〜/查看/共享/ EditorTemplates / ItemsViewModel。 CSHTML
。注意,模板的名称和位置是很重要的:
Now all that's left is write a custom editor template for the ItemsViewModel
which should be placed in ~/Views/Shared/EditorTemplates/ItemsViewModel.cshtml
. Notice that the name and location of the template is important:
@model ItemViewModel
@Html.DropDownListFor(x => x.SelectedValue, Model.Values)
这就是pretty太多所有需要。
and that's pretty much all it takes.
这篇关于@ Html.EditorForModel()下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!