asp.net-mvc ASP.NET MVC 禁用 Razor 语法的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14815821/
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
ASP.NET MVC disable radio buttons with Razor syntax
提问by A Bogus
In an ASP.NET MVC application, I have two radio buttons. Depending on a boolean value in the model, How do I enable or disable the radio buttons? (The values of the radio buttons are also part of the model)
在 ASP.NET MVC 应用程序中,我有两个单选按钮。根据模型中的布尔值,如何启用或禁用单选按钮?(单选按钮的值也是模型的一部分)
My radio buttons currently look like this -
我的单选按钮目前看起来像这样 -
@Html.RadioButtonFor(m => m.WantIt, "false")
@Html.RadioButtonFor(m => m.WantIt, "true")
In the model, I have a property called model.Alive. If model.Aliveis trueI want to enablethe radio buttons, else if model.Aliveis false, I want to disablethe radio buttons.
Thanks!
在模型中,我有一个名为model.Alive. 如果model.Alive是true我想启用单选按钮,否则如果model.Alive是false,我想禁用单选按钮。谢谢!
回答by amhed
You could pass the values directly as htmlAttributes like so:
您可以直接将值作为 htmlAttributes 传递,如下所示:
@Html.RadioButtonFor(m => m.WantIt, "false", new {disabled = "disabled"})
@Html.RadioButtonFor(m => m.WantIt, "true", new {disabled = "disabled"})
If you need to check for model.Alive then you could do something like this:
如果你需要检查 model.Alive 那么你可以做这样的事情:
@{
var htmlAttributes = new Dictionary<string, object>();
if (Model.Alive)
{
htmlAttributes.Add("disabled", "disabled");
}
}
Test 1 @Html.RadioButton("Name", "value", false, htmlAttributes)
Test 2 @Html.RadioButton("Name", "value2", false, htmlAttributes)
Hope that helps
希望有帮助
回答by sankaa
My answer would be the same as ahmed's. The only problem is, that WantIt property will not be sent on submit as it is ignored due to disabled html attribute. The solution is to add a HiddenFor above the RadioButtonFors like this:
我的答案将与艾哈迈德的答案相同。唯一的问题是,WantIt 属性不会在提交时发送,因为由于禁用了 html 属性,它会被忽略。解决方案是在 RadioButtonFors 上方添加一个 HiddenFor ,如下所示:
@Html.HiddenFor(m => m.WantIt)
@Html.RadioButtonFor(m => m.WantIt, "false", new {disabled = "disabled"})
@Html.RadioButtonFor(m => m.WantIt, "true", new {disabled = "disabled"})
That way all the values are rendered and you get the boolean back on submit.
这样所有的值都被呈现出来,并且你在提交时返回布尔值。
回答by Stewart Alan
Or provide an overload for RadioButtonFor?
或者为 RadioButtonFor 提供一个重载?
public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes)
{
var linkAttributes = System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
Dictionary<string, object> htmlAttributesDictionary = new Dictionary<string, object>();
foreach (var a in linkAttributes)
{
if (a.Key.ToLower() != "disabled")
{
htmlAttributesDictionary.Add(a.Key, a.Value);
}
}
if (isDisabled)
{
htmlAttributesDictionary.Add("disabled", "disabled");
}
return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, htmlAttributesDictionary);
}

