Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:39:54+00:00 2026-06-05T07:39:54+00:00

I am working in ASP.NET MVC3 using Razor. I have a situation where I

  • 0

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; }

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

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:39:57+00:00Added an answer on June 5, 2026 at 7:39 am

    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).

    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; }
    }
    

    and in /Shared/EditorTemplates/InstructorProperty.cshtml

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

    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() });        
    

    But I get that everyone has different styles… One other side note. If your aversion to using the Checkbox method is related to the generated naming scheme, the way that the Mvc Framework accesses this feature involves html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage I am working on using asp.net mvc3. I am deploying
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2
I'm working on a page using ASP.NET MVC3, Razor and Entity Framework. I used
My team working in a project using asp.net mvc3(c#). Based on the project requirement,we
I working on asp.net mvc3 project. I am using autofac for DI. I have
I have been working on ASP.net MVC3 Web application using MSSQL 2008 R2 for
We've been working with ASP.Net MVC2 based web app. We're using DataAnnotations for model
I'm using jquerymobile and asp.net MVC3 razor. I'm somewhat new to this, but figuring
I'm working on a web application project using ASP.NET MVC3 and database in SQL
I am working on a web-app with ASP.NET MVC3. We are using nHibernate with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.