ASP.NET MVC3 @ Html.EditorFor复选框来禁用启用 [英] ASP.NET MVC3 @Html.EditorFor Checkbox disable enable

查看:113
本文介绍了ASP.NET MVC3 @ Html.EditorFor复选框来禁用启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC3使用剃刀工作。我有,我想启用禁用基于一个布尔属性的复选框的情况。我的模型类有像2个属性:

I am working in ASP.NET MVC3 using Razor. I have a situation where I want to enable disable a checkbox based on a boolean property. My model class has 2 properties like:

public bool IsInstructor { get; set; }
public bool MoreInstructorsAllowed { get; set; }

现在在我的CSHTML文件,我显示复选框为:

Now in my cshtml file, I am showing the checkbox as:

@Html.EditorFor(model => model.IsInstructor)

我想此复选框上MoreInstructorsAllowed财产的基础上启用禁用。
感谢您事先的解决方案。 :)

I want this checkbox to enable disable on basis of MoreInstructorsAllowed property. Thanks in advance for the solution. :)

推荐答案

EditorFor 扩展方法电线您的模型是位于EditorTemplates的PartialView文件对应示范的类型(因此在这种情况下,将需要 Boolean.cshtml )。

The EditorFor extension method wires up your Model to the PartialView that is located in the EditorTemplates file that corresponds to the type of the Model (so in this case, it would need to be Boolean.cshtml).

您可以通过添加条件逻辑编辑模板实现你的目标。您还需要给部分一个办法知道 MoreInstructorsAllowed 酒店有什么样的价值,你可以使用 EditorFor 超载与 additionalViewData 参数来传递这些信息。

You can accomplish your objective by adding conditional logic to the Editor Template. You will also need to give the partial a way to know what value the MoreInstructorsAllowed property has, and you can use the EditorFor overload with the additionalViewData parameter to pass this information.

老实说,改变加工布尔默认功能似乎是它的一个小之多是为了什么你正在尝试做的。如果这两个字段是根本联系,它会更有意义,使两个场的复合物,并连线到复合而不是到布尔值本身的局部视图。我的意思是:

Honestly, changing the default functionality of processing booleans seems like its a little much for what you are trying to do. If these two fields are fundamentally linked, it would make more sense to make a composite of the two fields and wire up a partial view to the composite rather than the to the booleans themselves. What I mean is:

public class InstructorProperty { 
    public bool IsInstructor { get; set; }
    public bool MoreInstructorsAllowed { get; set; }
}

/Shared/EditorTemplates/InstructorProperty.cshtml

@model InstructorProperty

//  ...  Your view logic w/ the @if(MoreInstructorsClause) here.

唯一的问题是,现在你又回到不必使用 CheckboxFor 法,以申请已禁用属性的问题,因为 EditorFor 方法不接受临时HTML属性。这是一个已知的工作围绕涉及您的覆盖 ModelMetadataProvider 装潢和您提供的处理在ModelMetadataProvider属性的属性。这种技术的工作示例,请访问:<一个href=\"http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-_2D00_-ASP.Net-MVC-2.0-Beta_2D00_1.aspx\" rel=\"nofollow\">http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-2D00-ASP.Net-MVC-2.0-Beta_2D00_1.aspx 。然而,还是会涉及到两种:(1)覆盖布尔认为,要么硬编码的HTML或有使用CheckboxFor,(2)使用在 CheckboxFor 方法在 InstructorProperty 视图,或(3)硬编码的HTML到 InstructorProperty 视图。我不认为这是有道理的,这样一件微不足道的小事,过度复杂的设计,所以我的解决办法是使用 InstructorProperty 视图,只需添加:

The only problem is that now you are back at the problem of having to use the CheckboxFor method in order to apply the "disabled" attribute, because the EditorFor methods don't accept ad hoc html attributes. There is a known work around that involves overriding your ModelMetadataProvider and decorating the property with an attribute that you provide handling for in the ModelMetadataProvider. A working example of this technique is available at: http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-2D00-ASP.Net-MVC-2.0-Beta_2D00_1.aspx . However, that's still going to involve either: (1) overriding the Boolean view and either hard-coding the html or using CheckboxFor in there, (2) using a CheckboxFor method in the InstructorProperty view, or (3) hard-coding the html into the InstructorProperty view. I don't think it makes sense to over complicate design for such a trivial thing, so my solution would be to use this InstructorProperty view and just add:

@Html.CheckboxFor(_=>_.IsInstructor, 
    htmlAttributes: (Model.MoreInstructorsAllowed ? null : new { disabled = "disabled"} ).ToString().ToLowerInvariant() });        

但是我得到的,每个人都有不同的风格......另外一个方面说明。如果你所厌恶的使用复选框方法是关系到产生的命名方案,即MVC框架访问此功能的方式包括 html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)

这篇关于ASP.NET MVC3 @ Html.EditorFor复选框来禁用启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆