DMu*_*gan 92 nullable razor asp.net-mvc-3
我的模型有一个必须可以为空的布尔值
public bool? Foo
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
所以在我的Razor cshtml中
@Html.CheckBoxFor(m => m.Foo)
Run Code Online (Sandbox Code Playgroud)
除了不起作用.也没有(bool)施放它.如果我做
@Html.CheckBoxFor(m => m.Foo.Value)
Run Code Online (Sandbox Code Playgroud)
这不会产生错误,但在发布时它不会绑定到我的模型,并且foo设置为null.什么是在页面上显示Foo并使其在帖子上绑定到我的模型的最佳方式?
DMu*_*gan 101
我得到它与合作
@Html.EditorFor(model => model.Foo)
Run Code Online (Sandbox Code Playgroud)
然后在我的EditorTemplates文件夹中制作一个Boolean.cshtml并坚持下去
@model bool?
@Html.CheckBox("", Model.GetValueOrDefault())
Run Code Online (Sandbox Code Playgroud)
内.
res*_*kiy 40
在类似的问题中找到答案 - 将Nullable Bool渲染为CheckBox.这非常简单,只是有效:
@Html.CheckBox("RFP.DatesFlexible", Model.RFP.DatesFlexible ?? false)
@Html.Label("RFP.DatesFlexible", "My Dates are Flexible")
Run Code Online (Sandbox Code Playgroud)
这就像@afinkelstein接受的回答,除了我们不需要特殊的'编辑模板'
小智 25
我有bool? IsDisabled { get; set; }
模特.如果在视图中插入.
<div class="inputClass" id="disabled">
<div>
@if(Model.IsDisabled==null)
{
Model.IsDisabled = false;
}
@Html.CheckBoxFor(model => model.IsDisabled.Value)
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 13
我的模型有一个必须可以为空的布尔值
为什么?这没有意义.复选框有两种状态:已选中/未选中,如果有,则为真/假.没有第三国.
或者等待您在视图中使用域模型而不是视图模型?那是你的问题.因此,我的解决方案是使用视图模型,您将在其中定义一个简单的布尔属性:
public class MyViewModel
{
public bool Foo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,您将让控制器操作将此视图模型传递给视图并生成正确的复选框.
使基元与隐藏字段复杂化以阐明是否不建议使用False或Null.
Checkbox不是你应该使用的 - 它实际上只有一个状态:Checked.否则,它可能是任何东西.
当你的数据库字段是一个可以为空的boolean(bool?
)时,UX应该使用3-Radio Buttons,其中第一个按钮代表你的"Checked",第二个按钮代表"Not Checked",第三个按钮代表你的null,无论语义是什么null意味着.您可以使用<select><option>
下拉列表来保存房地产,但用户必须单击两次,选项几乎不会立即清除.
1 0 null
True False Not Set
Yes No Undecided
Male Female Unknown
On Off Not Detected
Run Code Online (Sandbox Code Playgroud)
RadioButtonList,定义为名为RadioButtonForSelectList的扩展,为您构建单选按钮,包括选中/选中的值,并设置,<div class="RBxxxx">
以便您可以使用css使单选按钮变为水平(显示:内联块),垂直或以表格方式(显示:内联块;宽度:100px;)
在模型中(我使用字符串,字符串定义的字符串作为教学示例.你可以使用bool?,string)
public IEnumerable<SelectListItem> Sexsli { get; set; }
SexDict = new Dictionary<string, string>()
{
{ "M", "Male"},
{ "F", "Female" },
{ "U", "Undecided" },
};
//Convert the Dictionary Type into a SelectListItem Type
Sexsli = SexDict.Select(k =>
new SelectListItem
{
Selected = (k.Key == "U"),
Text = k.Value,
Value = k.Key.ToString()
});
<fieldset id="Gender">
<legend id="GenderLegend" title="Gender - Sex">I am a</legend>
@Html.RadioButtonForSelectList(m => m.Sexsli, Model.Sexsli, "Sex")
@Html.ValidationMessageFor(m => m.Sexsli)
</fieldset>
public static class HtmlExtensions
{
public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> listOfValues,
String rbClassName = "Horizontal")
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var sb = new StringBuilder();
if (listOfValues != null)
{
// Create a radio button for each item in the list
foreach (SelectListItem item in listOfValues)
{
// Generate an id to be given to the radio button field
var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
// Create and populate a radio button using the existing html helpers
var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
var radio = String.Empty;
if (item.Selected == true)
{
radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id, @checked = "checked" }).ToHtmlString();
}
else
{
radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();
}// Create the html string to return to client browser
// e.g. <input data-val="true" data-val-required="You must select an option" id="RB_1" name="RB" type="radio" value="1" /><label for="RB_1">Choice 1</label>
sb.AppendFormat("<div class=\"RB{2}\">{0}{1}</div>", radio, label, rbClassName);
}
}
return MvcHtmlString.Create(sb.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,解决方案是更改视图模型。假设您正在搜索发票。这些发票可以支付也可以不支付。因此,您的搜索有三个选项:付费、未付费或“我不在乎”。
我最初将其设置为布尔值?场地:
public bool? PaidInvoices { get; set; }
Run Code Online (Sandbox Code Playgroud)
这让我偶然想到了这个问题。我最终创建了一个枚举类型,并按如下方式处理:
@Html.RadioButtonFor(m => m.PaidInvoices, PaidStatus.NotSpecified, new { @checked = true })
@Html.RadioButtonFor(m => m.PaidInvoices, PaidStatus.Yes)
@Html.RadioButtonFor(m => m.PaidInvoices, PaidStatus.No)
Run Code Online (Sandbox Code Playgroud)
当然,我将它们包裹在标签中并指定了文本,我只是说这是要考虑的另一种选择。
归档时间: |
|
查看次数: |
91449 次 |
最近记录: |