asp.net-mvc 将布尔属性编辑器转换为 MVC 视图中的下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15479791/
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
Convert a boolean property editor to a dropdownlist in MVC view
提问by Mister Epic
I currently have scaffolded a view where a boolean property of my model gets passed to the Html.EditorFor helper:
我目前已经搭建了一个视图,其中我的模型的布尔属性被传递给 Html.EditorFor 助手:
@Html.EditorFor(model => model.EndCurrentDeal)
All well and good, but what I'm really looking to do is massage that into a dropdown like:
一切都很好,但我真正想做的是将其按摩到如下下拉列表中:
<select>
<option value="true" selected="selected">Yes</option>
<option value="false">No</option>
</select>
What's the easiest way to achieve that?
实现这一目标的最简单方法是什么?
Thanks,
谢谢,
Chris
克里斯
采纳答案by Francois Borgies
You can try something like here:
你可以在这里尝试类似的东西:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
If you want a default Value :
如果你想要一个默认值:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "", Text = "None" },
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
回答by Serge
MVC 4
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
))
回答by Adam Bielecki
You can also try Dictionary approach is a bit cleaner and can be easily added to your constructor for View Model.
您还可以尝试使用 Dictionary 方法更简洁一些,并且可以轻松地将其添加到 View Model 的构造函数中。
ViewModel
视图模型
//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"))
回答by jyrkim
I got inspired with the very useful answer from Francois Borgies, so I decided to write a custom method that creates SelectListfor Booleanvalue that can be used in @Html.DropDownList. When you have a helper method that can be used in every view, then it reduces the amount of code needed in razor views.
我从 Francois Borgies 的非常有用的答案中得到了启发,所以我决定编写一个自定义方法,为可在@Html.DropDownList 中使用的布尔值 创建SelectList。当你有一个可以在每个视图中使用的辅助方法时,它就会减少剃刀视图中所需的代码量。
My project has CustomHelpers.csclass in folder: App_Code/Helpers
我的项目在文件夹中有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;
}
}
}
In the createview you can use it as follows:
在创建视图中,您可以按如下方式使用它:
@model Foo
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(), "-select-")
for editview
用于编辑视图
@model Bar
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(Model.EndCurrentDeal), "-select-")
Although my helper method is not pure HTML Helper because it creates SelectList, - it nevertheless follows same functionality that was presented by Rahul Rajat Singh, in his excellent article An Absolute Beginner's Tutorial on HTML Helpers and Creating Custom HTML Helpers in ASP.NET MVC
虽然我的助手方法不是纯 HTML 助手因为它创建了SelectList,但它仍然遵循 Rahul Rajat Singh 在他的优秀文章An Absolute Beginner's Tutorial on HTML Helpers 和 Creating Custom HTML Helpers in ASP.NET MVC 中介绍的相同功能

