5

How do we specify the class of Html.CheckBox ?

I am trying something like this, but this doesn't work.

   @Html.CheckBox("chk_", abc.IsChecked, new { @class="checkbox" })

THanks !!

5
  • This should work just fine! what is the output you are getting from this? Commented Aug 14, 2012 at 18:28
  • 1
    This is what i am getting as an error: CS1928: 'System.Web.Mvc.HtmlHelper<BCVA.Models.ABC>' 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 Commented Aug 14, 2012 at 19:16
  • 1
    the only thing I can think of is that IsChecked is not a bool! Commented Aug 14, 2012 at 19:39
  • @Html.CheckBox("chk_", abc.IsChecked) : This works fine, but when i add the new class attribute, it fails. Commented Aug 14, 2012 at 19:55
  • forgot to mention .. if you open the errors window it will tell you which one of your input is incorrect (e.g. could not cast type X to Y) Commented Aug 15, 2012 at 3:19

1 Answer 1

10

When specifying class, compiler puts a check to confirm that the bool value is not nullable, i fixed the datatype from null-able bool to bool, and it ran fine.

@Html.CheckBox("chk_", abc.IsChecked, new { @class="checkbox" })

Sign up to request clarification or add additional context in comments.

Comments

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.