asp.net-mvc 将 HTML.EditorFor 转换为下拉列表 (html.dropdownfor?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9517627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting HTML.EditorFor into a drop down (html.dropdownfor?)
提问by ZVenue
Currently I am using a Html.EditorFor control in a default 'Create' View page like this.
目前,我在像这样的默认“创建”视图页面中使用 Html.EditorFor 控件。
<%: Html.EditorFor(model => model.IsActive) %>
I would like to convert this to a drop down with values and still be binded to the model in the view. My question is two fold.
我想将其转换为带有值的下拉列表,并且仍然绑定到视图中的模型。我的问题有两个方面。
If there are only 2/3 values needed in the drop down..Is there a quick way to explicitly populate 2 or 3 values?
If the list is big and needs to come from a sql query, how to do this?
如果下拉列表中只需要 2/3 个值..有没有一种快速的方法来显式填充 2 个或 3 个值?
如果列表很大并且需要来自 sql 查询,该怎么做?
Thanks in advance for the help.
在此先感谢您的帮助。
回答by Darin Dimitrov
In order to generate a dropdownlist you need 2 properties on your view model: a scalar property to bind the selected value to and a collection property which will contain the items to show in the dropdown.
为了生成下拉列表,您的视图模型需要 2 个属性:一个用于将所选值绑定到的标量属性和一个包含要在下拉列表中显示的项目的集合属性。
So you could define a view model:
所以你可以定义一个视图模型:
public class DropDownListViewModel
{
public string SelectedValue { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
and then on your main view model have a property of this type:
然后在你的主视图模型上有一个这种类型的属性:
public DropDownListViewModel Foo { get; set; }
Now you could have a custom editor template for this type (~/Views/Shared/EditorTemplates/DropDownListViewModel.ascx):
现在,您可以拥有此类型的自定义编辑器模板 ( ~/Views/Shared/EditorTemplates/DropDownListViewModel.ascx):
<%@ Control
Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DropDownListViewModel>"
%>
<%= Html.DropDownListFor(x => x.SelectedValue, Model.Items) %>
and then in your main view:
然后在您的主视图中:
<%= Html.EditorFor(x => x.Foo) %>
Now all that's left is to have your controller action rendering the main view to fill the Fooproperty with the corresponding values. The could be hardcoded, come from a repository or whatever. It doesn't really matter.
现在剩下的就是让您的控制器操作呈现主视图以Foo使用相应的值填充属性。可以是硬编码的,来自存储库或其他任何东西。这并不重要。
On the other hand if you knew the values in advance you could hardcode them in the editor template (~/Views/Shared/EditorTemplates/YesNoDropDown.ascx):
另一方面,如果您事先知道这些值,您可以在编辑器模板 ( ~/Views/Shared/EditorTemplates/YesNoDropDown.ascx)中对它们进行硬编码:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
and then:
进而:
<%= Html.EditorFor(x => x.IsActive, "YesNoDropDown") %>
or by decorating the IsActive property on your view model:
或者通过在你的视图模型上装饰 IsActive 属性:
[UIHint("YesNoDropDown")]
public bool IsActive { get; set; }
and then:
进而:
<%= Html.EditorFor(x => x.IsActive) %>

