Checkbox disabled attribute in ASP.NET MVC












39















My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false. What is the proper razor syntax to accomplish this ?



I tried the code below on a list of items in a table. Every row comes back with a disabled checkbox regardless of selectable value.



 @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = !item.Selectable })









share|improve this question




















  • 1





    Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

    – EfrainReyes
    Jan 6 '14 at 0:14


















39















My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false. What is the proper razor syntax to accomplish this ?



I tried the code below on a list of items in a table. Every row comes back with a disabled checkbox regardless of selectable value.



 @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = !item.Selectable })









share|improve this question




















  • 1





    Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

    – EfrainReyes
    Jan 6 '14 at 0:14
















39












39








39


2






My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false. What is the proper razor syntax to accomplish this ?



I tried the code below on a list of items in a table. Every row comes back with a disabled checkbox regardless of selectable value.



 @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = !item.Selectable })









share|improve this question
















My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false. What is the proper razor syntax to accomplish this ?



I tried the code below on a list of items in a table. Every row comes back with a disabled checkbox regardless of selectable value.



 @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = !item.Selectable })






c# asp.net-mvc razor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 6 '14 at 0:01









Dalorzo

16.2k64388




16.2k64388










asked Jan 5 '14 at 23:46









Bill GreerBill Greer

1,28952553




1,28952553








  • 1





    Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

    – EfrainReyes
    Jan 6 '14 at 0:14
















  • 1





    Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

    – EfrainReyes
    Jan 6 '14 at 0:14










1




1





Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

– EfrainReyes
Jan 6 '14 at 0:14







Question: What is item? I see on your lambda that you have modelItem => item.Selected, but is item independent from the model itself? Or perhaps you meant to say modelItem => modelItem.Selected?

– EfrainReyes
Jan 6 '14 at 0:14














3 Answers
3






active

oldest

votes


















70














It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.



<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">


This should work in the razor. Simple If condition and rendering what you want.



@if(item.Selected)
{
@Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
@Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}


You may consider writing a custom html helper which renders the proper markup for this.






share|improve this answer



















  • 2





    For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

    – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
    Feb 28 '17 at 15:56













  • stackoverflow.com/a/38366971/4594225

    – Ali
    Jul 14 '18 at 15:12



















24














This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be disabled.



Try something like this:



@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })


Note that you might need to cast to (object)






share|improve this answer


























  • This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

    – Ian
    Mar 4 '15 at 9:29








  • 1





    You might just be able to go new object() on the left side

    – Daniel Little
    Mar 4 '15 at 10:15



















1














Sorry, my previous answer was wrong.



The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.



So you will have to set the disabled attribute only when the condition is valid.



something like:



object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)





share|improve this answer


























  • I tried your recommendation. Everything comes back as disabled.

    – Bill Greer
    Jan 6 '14 at 0:02






  • 1





    you probably need @ prefixes

    – ealfonso
    Aug 4 '14 at 1:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20940746%2fcheckbox-disabled-attribute-in-asp-net-mvc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









70














It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.



<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">


This should work in the razor. Simple If condition and rendering what you want.



@if(item.Selected)
{
@Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
@Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}


You may consider writing a custom html helper which renders the proper markup for this.






share|improve this answer



















  • 2





    For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

    – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
    Feb 28 '17 at 15:56













  • stackoverflow.com/a/38366971/4594225

    – Ali
    Jul 14 '18 at 15:12
















70














It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.



<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">


This should work in the razor. Simple If condition and rendering what you want.



@if(item.Selected)
{
@Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
@Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}


You may consider writing a custom html helper which renders the proper markup for this.






share|improve this answer



















  • 2





    For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

    – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
    Feb 28 '17 at 15:56













  • stackoverflow.com/a/38366971/4594225

    – Ali
    Jul 14 '18 at 15:12














70












70








70







It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.



<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">


This should work in the razor. Simple If condition and rendering what you want.



@if(item.Selected)
{
@Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
@Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}


You may consider writing a custom html helper which renders the proper markup for this.






share|improve this answer













It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.



<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">


This should work in the razor. Simple If condition and rendering what you want.



@if(item.Selected)
{
@Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
@Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
}


You may consider writing a custom html helper which renders the proper markup for this.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 6 '14 at 0:03









ShyjuShyju

146k87331441




146k87331441








  • 2





    For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

    – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
    Feb 28 '17 at 15:56













  • stackoverflow.com/a/38366971/4594225

    – Ali
    Jul 14 '18 at 15:12














  • 2





    For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

    – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
    Feb 28 '17 at 15:56













  • stackoverflow.com/a/38366971/4594225

    – Ali
    Jul 14 '18 at 15:12








2




2





For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

– ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
Feb 28 '17 at 15:56







For HTML5 I write new { @disabled = "" } as <input type="checkbox" disabled /> is valid and used by W3C in their samples.

– ᴍᴀᴛᴛ ʙᴀᴋᴇʀ
Feb 28 '17 at 15:56















stackoverflow.com/a/38366971/4594225

– Ali
Jul 14 '18 at 15:12





stackoverflow.com/a/38366971/4594225

– Ali
Jul 14 '18 at 15:12













24














This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be disabled.



Try something like this:



@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })


Note that you might need to cast to (object)






share|improve this answer


























  • This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

    – Ian
    Mar 4 '15 at 9:29








  • 1





    You might just be able to go new object() on the left side

    – Daniel Little
    Mar 4 '15 at 10:15
















24














This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be disabled.



Try something like this:



@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })


Note that you might need to cast to (object)






share|improve this answer


























  • This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

    – Ian
    Mar 4 '15 at 9:29








  • 1





    You might just be able to go new object() on the left side

    – Daniel Little
    Mar 4 '15 at 10:15














24












24








24







This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be disabled.



Try something like this:



@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })


Note that you might need to cast to (object)






share|improve this answer















This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be disabled.



Try something like this:



@Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ?  (object)new {} :  (object)new { @disabled = "disabled" })


Note that you might need to cast to (object)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 23:21









ealfonso

3,01222239




3,01222239










answered Jan 6 '14 at 0:03









Daniel LittleDaniel Little

11.9k95683




11.9k95683













  • This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

    – Ian
    Mar 4 '15 at 9:29








  • 1





    You might just be able to go new object() on the left side

    – Daniel Little
    Mar 4 '15 at 10:15



















  • This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

    – Ian
    Mar 4 '15 at 9:29








  • 1





    You might just be able to go new object() on the left side

    – Daniel Little
    Mar 4 '15 at 10:15

















This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

– Ian
Mar 4 '15 at 9:29







This is a much cleaner option than the selected answer. You do need to cast as object; in my case I had this working: @Html.EditorFor(p => item.Selected, new {htmlAttributes = item.Selectable ? (object)new { } : (object)new { @disabled = "disabled" }})

– Ian
Mar 4 '15 at 9:29






1




1





You might just be able to go new object() on the left side

– Daniel Little
Mar 4 '15 at 10:15





You might just be able to go new object() on the left side

– Daniel Little
Mar 4 '15 at 10:15











1














Sorry, my previous answer was wrong.



The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.



So you will have to set the disabled attribute only when the condition is valid.



something like:



object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)





share|improve this answer


























  • I tried your recommendation. Everything comes back as disabled.

    – Bill Greer
    Jan 6 '14 at 0:02






  • 1





    you probably need @ prefixes

    – ealfonso
    Aug 4 '14 at 1:33
















1














Sorry, my previous answer was wrong.



The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.



So you will have to set the disabled attribute only when the condition is valid.



something like:



object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)





share|improve this answer


























  • I tried your recommendation. Everything comes back as disabled.

    – Bill Greer
    Jan 6 '14 at 0:02






  • 1





    you probably need @ prefixes

    – ealfonso
    Aug 4 '14 at 1:33














1












1








1







Sorry, my previous answer was wrong.



The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.



So you will have to set the disabled attribute only when the condition is valid.



something like:



object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)





share|improve this answer















Sorry, my previous answer was wrong.



The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.



So you will have to set the disabled attribute only when the condition is valid.



something like:



object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
@Html.CheckBoxFor(modelItem => item.Selected, attributes)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 6 '14 at 0:03

























answered Jan 5 '14 at 23:58









PbirkoffPbirkoff

3,90121518




3,90121518













  • I tried your recommendation. Everything comes back as disabled.

    – Bill Greer
    Jan 6 '14 at 0:02






  • 1





    you probably need @ prefixes

    – ealfonso
    Aug 4 '14 at 1:33



















  • I tried your recommendation. Everything comes back as disabled.

    – Bill Greer
    Jan 6 '14 at 0:02






  • 1





    you probably need @ prefixes

    – ealfonso
    Aug 4 '14 at 1:33

















I tried your recommendation. Everything comes back as disabled.

– Bill Greer
Jan 6 '14 at 0:02





I tried your recommendation. Everything comes back as disabled.

– Bill Greer
Jan 6 '14 at 0:02




1




1





you probably need @ prefixes

– ealfonso
Aug 4 '14 at 1:33





you probably need @ prefixes

– ealfonso
Aug 4 '14 at 1:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20940746%2fcheckbox-disabled-attribute-in-asp-net-mvc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?