我目前已经搭建了一个视图,在这个视图中,我的模型的布尔值属性被传递给Html.EditorFor助手:
@Html.EditorFor(model => model.EndCurrentDeal)一切都很好,但我真正想做的是将其导入下拉列表中,如下所示:
<select>
<option value="true" selected="selected">Yes</option>
<option value="false">No</option>
</select>实现这一目标的最简单方法是什么?
谢谢,
克里斯
发布于 2013-03-18 22:57:56
你可以尝试像here这样的东西
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>如果您想要一个默认值:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "", Text = "None" },
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>发布于 2013-10-02 23:47:56
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
))发布于 2014-01-16 21:21:42
你也可以尝试使用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"))https://stackoverflow.com/questions/15479791
复制相似问题