7

I'm trying to disable a checkbox, but I'm getting an error and can't figure out what I'm doing wrong. My code is this

@Html.CheckBox("", ViewData.TemplateInfo.FormattedModelValue, new { @disabled = true } )

which as far as I can tell, judging by other explanations of how to disable a checkbox, should work. However, I'm getting this error:

CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'CheckBox' and the best extension method overload 'System.Web.Mvc.Html.InputExtensions.CheckBox(System.Web.Mvc.HtmlHelper, string, bool, object)' has some invalid arguments

Any ideas? Thank you.

1 Answer 1

10

The CheckBox helper expects a boolean value as second parameter. Try like this:

@Html.CheckBox(
    "", 
    bool.Parse((string)ViewData.TemplateInfo.FormattedModelValue), 
    new { disabled = "disabled" } 
)

or if this is a strongly typed editor template to boolean:

@model bool
@Html.CheckBox("", Model, new { disabled = "disabled" })
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, thank you. I understand what was happening now. Parsing the FormattedModelValue as a bool didn't work, because it was already a bool, but casting it as (bool)ViewData.TemplateInfo.FormattedModelValue did. Thanks again for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.