将 HTML.EditorFor 转换为下拉列表 (html.dropdownfor?) [英] Converting HTML.EditorFor into a drop down (html.dropdownfor?)

查看:34
本文介绍了将 HTML.EditorFor 转换为下拉列表 (html.dropdownfor?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在像这样的默认创建"视图页面中使用 Html.EditorFor 控件.

Currently I am using a Html.EditorFor control in a default 'Create' View page like this.

 <%: 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.

  1. 如果下拉列表中只需要 2/3 个值..有没有一种快速的方法来显式填充 2 个或 3 个值?

  1. If there are only 2/3 values needed in the drop down..Is there a quick way to explicitly populate 2 or 3 values?

如果列表很大,需要来自一个sql查询,怎么做?

If the list is big and needs to come from a sql query, how to do this?

预先感谢您的帮助.

推荐答案

为了生成下拉列表,您的视图模型需要 2 个属性:一个用于绑定选定值的标量属性和一个将包含项目的集合属性显示在下拉菜单中.

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.

所以你可以定义一个视图模型:

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; }

现在您可以为这种类型创建一个自定义编辑器模板 (~/Views/Shared/EditorTemplates/DropDownListViewModel.ascx):

Now you could have a custom editor template for this type (~/Views/Shared/EditorTemplates/DropDownListViewModel.ascx):

<%@ Control 
    Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DropDownListViewModel>" 
%>
<%= Html.DropDownListFor(x => x.SelectedValue, Model.Items) %>

然后在您的主视图中:

<%= Html.EditorFor(x => x.Foo) %> 

现在剩下的就是让您的控制器操作呈现主视图以使用相应的值填充 Foo 属性.可以是硬编码的,来自存储库或其他任何东西.没关系.

Now all that's left is to have your controller action rendering the main view to fill the Foo property with the corresponding values. The could be hardcoded, come from a repository or whatever. It doesn't really matter.

另一方面,如果您事先知道这些值,您可以在编辑器模板中对它们进行硬编码 (~/Views/Shared/EditorTemplates/YesNoDropDown.ascx):

On the other hand if you knew the values in advance you could hardcode them in the editor template (~/Views/Shared/EditorTemplates/YesNoDropDown.ascx):

<%= Html.DropDownList(
    "", 
    new SelectList(
        new[] 
        { 
            new { Value = "true", Text = "Yes" },
            new { Value = "false", Text = "No" },
        }, 
        "Value", 
        "Text",
        Model
    )
) %>

然后:

<%= Html.EditorFor(x => x.IsActive, "YesNoDropDown") %> 

或者通过在你的视图模型上装饰 IsActive 属性:

or by decorating the IsActive property on your view model:

[UIHint("YesNoDropDown")]
public bool IsActive { get; set; }

然后:

<%= Html.EditorFor(x => x.IsActive) %> 

这篇关于将 HTML.EditorFor 转换为下拉列表 (html.dropdownfor?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆