Search code examples
c#asp.net-mvc-3checkboxfor

Pre-check a box in a Create View in MVC 3 with C#


I'm working on a site using MVC 3 and C#. I have a simple boolean field that is set with a checkbox. I can create and edit just fine and the values of the boolean field update alright. My only question is: Can I make it so the CheckBox is automatically checked when the user reaches the page? It's a more common situation for it to be checked than not, so I'd like to make it that way. Right now I render the check box with:

@Html.CheckBoxFor(model => model.Tbd)

I have tried:

@Html.CheckBoxFor(model => model.Tbd, new { value = true } )

And:

@Html.CheckBoxFor(model => model.Tbd, new { value = "true" } )

Neither of these three change the actual look of the page, but the HTML source is changed a bit (namely the value field for the Check Box input tag changes from: value = "true" to value = "True"). So these three methods don't work. So again, can I make it so the checkbox is pre-checked when a user reaches a page for the first time? (Do note this is for Creation, in the Edit View if Tbd is true, the check box is checked like it should be)


Solution

  • What is the value of model.Tbd? If it's not already True, try init it to True and seeing if that makes a difference