Rhonda
Rhonda

Reputation: 985

MVC3 Check a checkbox on page load

I am working on an MVC3 app that has several forms. When I get to one form if a variable (ViewData) is true I want to check a checkbox by default but I have no idea how to do this. Any help?

Thanks,

Rhonda

Upvotes: 1

Views: 2283

Answers (2)

fearofawhackplanet
fearofawhackplanet

Reputation: 53396

With the HtmlHelper extension :

<% Html.CheckBox("name", bool.Parse(ViewData["isChecked"])) %>

Upvotes: 2

Michael Kennedy
Michael Kennedy

Reputation: 3380

I would recommend using Html.CheckboxFor(). Here's a simple example. If you have a model with a property IsSet then you can do this:

@Html.CheckBoxFor(m => m.IsSet)

Good luck.

Upvotes: 3

Related Questions