简体   繁体   English

ASP.NET MVC 中的复选框禁用属性

[英]Checkbox disabled attribute in ASP.NET MVC

My ViewModel has a property of selected and selectable.我的 ViewModel 具有 selected 和 selectable 属性。 Both are boolean.两者都是布尔值。 I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false.我希望我的视图有一个复选框,当 selectable 为 true 时启用,当 selectable 为 false 时禁用。 What is the proper razor syntax to accomplish this ?完成此操作的正确剃刀语法是什么?

I tried the code below on a list of items in a table.我在表格中的项目列表上尝试了下面的代码。 Every row comes back with a disabled checkbox regardless of selectable value.无论可选值如何,每一行都会返回一个禁用的复选框。

 @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = !item.Selectable })

It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.这是不容易有实现这个if辅助方法内部条件,因为所有下面的标记将呈现一个残疾人chechbox。

<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">

This should work in the razor.这应该适用于剃须刀。 Simple If condition and rendering what you want.简单的条件和渲染你想要的。

@if(item.Selected)
{ 
  @Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
    @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}

You may consider writing a custom html helper which renders the proper markup for this.您可以考虑编写一个自定义 html 帮助程序,它为此呈现正确的标记。

This won't work because <input disabled="anything" /> will result in a disabled control.这将不起作用,因为<input disabled="anything" />将导致禁用控件。 You need to only have a @disabled property when it should be disabled.当它应该被禁用时,您只需要拥有一个 @disabled 属性。

Try something like this:尝试这样的事情:

@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })

Note that you might need to cast to (object)请注意,您可能需要强制转换为(object)

The problem is when you have to add more than 1 HTML attribute.问题是当您必须添加 1 个以上的 HTML 属性时。 That's a mess:这是一团糟:

@if(item.Selected)
{ 
  @Html.CheckBoxFor(modelItem => item.Selected, new { @data_foo = "bar"})
}
else
{
    @Html.CheckBoxFor(modelItem => item.Selected, new { @data_foo = "bar", @disabled = "disabled"})
}

What I do to solve this is to use a IDictionary<string, object> that is previously loaded:我为解决这个问题所做的是使用以前加载的IDictionary<string, object>

var htmlAttributes = new Dictionary<string, object>{
    {"data-foo", "bar"}
};
if(!item.Selected)
{
    htmlAttributes.Add("disabled", "disabled");
}

And then I create the checkbox component only once:然后我只创建一次复选框组件:

@Html.CheckBoxFor(modelItem => item.Selected, htmlAttributes)

Sorry, my previous answer was wrong.对不起,我之前的回答是错误的。

The input-element will be disabled as soon as it gets the attribute disabled.一旦禁用属性,输入元素将被禁用。 It doesn't matter if the value is true, false.值是真还是假都没有关系。 In HTML you can't set disabled to false.在 HTML 中,您不能将 disabled 设置为 false。

So you will have to set the disabled attribute only when the condition is valid.因此,您必须仅在条件有效时设置 disabled 属性。

something like:就像是:

object attributes = null;
if (!item.Selectable)
{
    attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM