转换成HTML.EditorFor一个下拉(html.dropdownfor?) [英] Converting HTML.EditorFor into a drop down (html.dropdownfor?)

查看:136
本文介绍了转换成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在下拉所需值down..Is有一个快速的方法来明确填充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个属性一个DropDownList:标量属性选择的值结合并集合属性将包含的项目显示在下拉列表中。

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

现在你可以为这种类型的自定义编辑器模板(〜/查看/共享/ 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) %>

,然后在你的主观点:

and then in your main view:

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

现在,所有剩下的是让你的控制器动作呈现主视图,以填补相应的值属性。在可能很难codeD,来自一个仓库或什么的。这其实并不重要。

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.

在另一方面,如果你事先知道的值,您可以硬code他们在编辑模板(〜/查看/共享/ 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天全站免登陆