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

编程入门 行业动态 更新时间:2024-10-28 10:25:01
本文介绍了ASP.NET MVC中禁用复选框的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的ViewModel具有selected和selectable的属性.两者都是布尔值.我希望我的视图有一个复选框,当selectable为true时启用,而selectable为false则禁用.完成此操作的合适剃刀语法是什么?

My ViewModel has a property of selected and 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. 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 })

推荐答案

使用if条件内部方法来实现此目标并不容易,因为以下所有标记都会导致禁用chechbox.

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.

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

这应该在剃须刀中起作用.简单的If条件并呈现所需的内容.

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"}) }

您可以考虑编写一个自定义html帮助器,以为此呈现适当的标记.

You may consider writing a custom html helper which renders the proper markup for this.

更多推荐

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

本文发布于:2023-10-12 01:41:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   属性   ASP   NET   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!