ASP.NET MVC3 @ Html.EditorFor check box disable enable

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

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

Now in my cshtml file, I show this flag as:

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

I want this checkbox to enable disable based on the MoreInstructorsAllowed property. Thanks in advance for the decision. :)

+5
source share
1 answer

The extension method EditorForconnects your model to PartialView, which is located in the EditorTemplates file, which corresponds to the type of model (therefore, in this case it should be Boolean.cshtml).

, . , MoreInstructorsAllowed, EditorFor additionalViewData .

, , . , , . :

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 "disabled", EditorFor ad hoc html. , ModelMetadataProvider , ModelMetadataProvider. : 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 :

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

, ... . Checkbox , , Mvc Framework , html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)

+3

All Articles