将布尔属性编辑器转换为MVC视图中的下拉列表

Mis*_*pic 24 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我目前已经搭建了一个视图,其中我的模型的布尔属性被传递给Html.EditorFor帮助器:

@Html.EditorFor(model => model.EndCurrentDeal)
Run Code Online (Sandbox Code Playgroud)

一切都很好,但我真正想要做的就是按下这样的下拉菜单:

<select>
    <option value="true" selected="selected">Yes</option>
    <option value="false">No</option>
</select>
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最简单方法是什么?

谢谢,

克里斯

Fra*_*ies 24

您可以尝试像在这里:

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

如果您想要默认值:

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


Ser*_*rge 21

MVC 4

@*/////////////////// bool ////////////////////////////////*@
@model bool

@Html.DropDownListFor(m => m, new SelectList(
        new[] 
        { 
            new { Value = "true", Text = "Yes" },
            new { Value = "false", Text = "No" },
        },
        "Value",
        "Text",
        Model
    ))

@*/////////////////// bool? ////////////////////////////////*@    
@model bool?

@Html.DropDownListFor(m => m, new SelectList(
        new[] 
        { 
            new { Value = "", Text = "(none)" },
            new { Value = "true", Text = "Yes" },
            new { Value = "false", Text = "No" },
        },
        "Value",
        "Text",
        Model
    ))
Run Code Online (Sandbox Code Playgroud)


Ada*_*cki 9

您还可以尝试使用Dictionary方法更简洁,并且可以轻松地添加到View Model的构造函数中.

视图模型

 //Define IDictionary interface
 public IDictionary<bool, string> options { get; set; }

public YourViewModel()
{
  // Default constructor
        this.options = new Dictionary<bool, string>();
        this.options.Add(false, "no");
        this.options.Add(true, "yes");
}




@Html.DropDownListFor(model => model.yourPropertyToEdit, new SelectList( Model.options, "Key", "Value"))
Run Code Online (Sandbox Code Playgroud)


jyr*_*kim 5

我从 Francois Borgies 的非常有用的答案中得到了启发,所以我决定编写一个自定义方法,为可在@Html.DropDownList 中使用的布尔值 创建SelectList。当你有一个可以在每个视图中使用的辅助方法时,它就会减少剃刀视图中所需的代码量。

我的项目在文件夹中有CustomHelpers.cs类:App_Code/Helpers

namespace YourProjectName.App_Code.Helpers
{
    public static class CustomHelpers
    {
        public static SelectList SelectListForBoolean(object selectedValue = null)
        {
            SelectListItem[] selectListItems = new SelectListItem[2];

            var itemTrue = new SelectListItem();
            itemTrue.Value = "true";
            itemTrue.Text = "Yes";
            selectListItems[0] = itemTrue;

            var itemFalse = new SelectListItem();
            itemFalse.Value = "false";
            itemFalse.Text = "No";
            selectListItems[1] = itemFalse;

            var selectList = new SelectList(selectListItems, "Value","Text", selectedValue);

            return selectList;
        }           
    }
}
Run Code Online (Sandbox Code Playgroud)

创建视图中,您可以按如下方式使用它:

@model Foo
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(), "-select-")
Run Code Online (Sandbox Code Playgroud)

用于编辑视图

@model Bar
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(Model.EndCurrentDeal), "-select-")
Run Code Online (Sandbox Code Playgroud)

虽然我的辅助方法不是纯 HTML 辅助方法,因为它创建了SelectList,但它遵循 Rahul Rajat Singh 在他的优秀文章An Absolute Beginner's Tutorial on HTML Helpers 和 Creating Custom HTML Helpers in ASP.NET MVC 中介绍的相同功能