asp.net-mvc Razor 如何创建 CheckBox 并使其成为 READONLY?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11308661/
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
Razor how to create a CheckBox and make it READONLY?
提问by GibboK
I'm using MVC 3 and Razor
我正在使用 MVC 3 和 Razor
At the moment I'm using
目前我正在使用
@model MyProject.ViewModels.MyViewModel
@foreach (var item in Model.MyProperty)
{
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.AdvSlotId }) |
@Html.ActionLink("Details", "Details", new { id = item.AdvSlotId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.AdvSlotId })
</td>
<td>
@item.AdvSlotId
</td>
<td>
@item.Name
</td>
<td>
@item.Description
</td>
<td>
@Html.CheckBox(item.IsPublished, new { @disabled = "disabled" })
</td>
<td>
@item.Notes
</td>
</tr>
}
The VIEW MODEL:
视图模型:
namespace MyProject.ViewModels
{
public class MyViewModel
{
public MyViewModel(List<AdvSlot> advSlots)
{
MyProperty= advSlots;
}
public List<AdvSlot> MyProperty { get; set; }
}
}
To display a Check Box for a property in my Model. As I'm doing is wrong so I can only display a text like TRUE.
在我的模型中显示属性的复选框。因为我这样做是错误的,所以我只能显示像 TRUE 这样的文本。
Could you please tell me how to create the CheckBox with Razor? I would also need have it as READONLY.
你能告诉我如何用 Razor 创建 CheckBox 吗?我还需要将其设为 READONLY。
Thanks for your help.
谢谢你的帮助。
回答by LeftyX
Should be something like this:
应该是这样的:
@Html.CheckBoxFor(m => m.IsPublished, new { @disabled = "disabled" })
UPDATE:
更新:
Assuming that your class AdvSlotcontains a property IsPublishedyou can write in your loop:
假设您的类AdvSlot包含IsPublished可以在循环中编写的属性:
<td>
@Html.CheckBox(item.AdvSlotId + "_IsPublished", item.IsPublished, new { @disabled = "disabled" });
</td>
回答by Anupam Roy
@Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })
This should work. However i got a problem with @disabled = "disabled". If i make the checkbox checked by default and make the checkbox disabled at the same time it wont post with its value. At controller it will get false. Using "return false" user wont able to change the value and it will also post the value which has set on load as by default.
这应该有效。但是我遇到了@disabled = "disabled" 的问题。如果我默认选中该复选框并同时禁用该复选框,则它不会发布其值。在控制器它会得到假。使用“return false”用户将无法更改该值,并且它还会发布默认情况下在加载时设置的值。
回答by outofmind
I'm using MVC4, you get the same effect with a simple something like
我正在使用 MVC4,你可以用一个简单的东西得到同样的效果
@Html.DisplayFor(m => m.IsPublished)
No need to care to select checkboxor set disabledyourself. Not sure, if this is a new feature in MVC4, so maybe it will work in MVC3 as well.
无需关心选择复选框或自己设置禁用。不确定,如果这是 MVC4 中的新功能,那么它可能也适用于 MVC3。
回答by Mohamed Farrag
In your MyViewModel you can remove the setter :
在您的 MyViewModel 中,您可以删除 setter :
public bool IsPublished
{
get {
//... get from db
}
}
and in your View :
并在您的视图中:
@Html.CheckBoxFor(m => m.IsPublished, new {@disabled="disabled"})

