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 7911507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:16:34+00:00 2026-06-03T13:16:34+00:00

For a view, I’ve to generate some checkbox. I’ve one collection of items: public

  • 0

For a view, I’ve to generate some checkbox.

I’ve one collection of items:

public class ItemSelection
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public Boolean IsSelected { get; set; }
        public Boolean IsActive { get; set; }
    }

and in the view, I’m iterating on this

@foreach(ItemSelection item in Model.Items){
   Html.CheckBoxFor(m=>item.IsSelected)//HERE I WOULD LIKE TO HAVE DISABLED properties if I've a IsActive=falsel
   Html.HiddenFor(m=>item.Id)
}

Now I see that I can do a “if” in which I create a different HtmlAttribute array, depending of this property, but is there a way to create only one array

new {disabled=item.IsActive?"ONE_SPECIAL_VALUE_HERE":"disabled"}

I tried to put false, or some other things, nothing worked.

  • 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-03T13:16:35+00:00Added an answer on June 3, 2026 at 1:16 pm

    You can’t avoid the if:

    The problem is with the special nature of the disabled attribute because there is no “special” value which would make your sample work, because:

    “disabled” is only possible value for this attribute. If the input
    should be enabled, simply omit the attribute entirely.

    So you need to omit the attribute to enable the control but all HTML helpers will serialize all the properties of the anonymous objects passed in as the htmlattributes. And there is no way to conditionally add properties to anonymous types.

    However if you have multiple common attributes for the enable/disable case, and you don’t want to create two anonymoues types, you can put the attributes in a dictionary with the optional disabled attribute and use the dictionary as the htmlAttributes:

    var checkboxHtmlAttributes = new Dictionary<string, object>
                                    {{"attibute1", "value1"}, 
                                    {"attribute2", "value2"}};
    if (!item.IsActive)
    {
        checkboxHtmlAttributes.Add("disabled", "disabled");
    }
    @Html.CheckBoxFor(m=>item.IsSelected, checkboxHtmlAttributes)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

View Model: public class Note { [DataType(DataType.MultilineText)] public string Text { get; set; }
view model: namespace AESSmart.ViewModels { public class HomeIndexViewModel { public power_weatherstationhistory WeatherStationHistory {get;set;} public
public View getView(int position, View convertView, ViewGroup parent) { Button btn; if (convertView ==
custom view: public class MyView extends AbstractView { .... awesome stuff ... } controller:
In view I'd like to bind some class, but this element should also has
My view is having issues finding a class located in one of my references.
view.py someForm = SomeForm(request.POST) ... someForm.customSave(request.user) forms.py class SomeForm(ModelForm): class Meta: model = Some
my View constructor: public View1(IRegionManager regionManager, IUnityContainer container, bool myParam) { } How to
My view calls some backend classes which need some user input. When user input
View Here: http://174.132.101.73/~ree/header/ I have a drop down box. I am trying to get

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.