getting checkboxfor value checked by default in asp.net mvc2
I have a model which has a field
[DisplayName("Receive occasional email notifications about new services or features")]
public bool ReceiveEmail { get; set; }
In my view I want a checkbox which will come checked by default.
I tried this:
<%:Html.开发者_开发问答CheckBoxFor(m => m.registerModel.ReceiveEmail, new { @checked = "checked" })%>
But did not work...
Any help will be appreciated.
Thanks Arnab
The proper way to do this is to set your view model property in the controller action rendering this view:
public ActionResult Foo()
{
var model = new MyViewModel
{
registerModel = new registerModel
{
ReceiveEmail = true
}
};
return View(model);
}
Now all you need in your strongly typed view is:
<%= Html.CheckBoxFor(m => m.registerModel.ReceiveEmail) %>
and the checkbox will be automatically checked.
精彩评论