I tried the follow sample code, it works to initialize the check box. I use MVC 3.0, you can convert it to mvc 2.0:
Model view:
public class Test
{
public bool Sunday { get; set; }
public bool Monday { get; set; }
public bool AllowSunday { get; set; }
public bool AllowMonday { get; set; }
}
Action:
public ActionResult search()
{
t.Monday = true;
t.AllowMonday = true;
t.Sunday = false;
t.AllowSunday = false;
return View(t);
}
[HttpPost]
public ActionResult search(Test m)
{
return RedirectToAction("search");
}
sachingusain
Star
8786 Points
1702 Posts
Re: How to use CheckboxFor to set checked property
Jan 28, 2011 09:18 PM|LINK
I would like to see code here because this is something that I have used and done without any issues.
What is the datatype of "Sunday" property on "search" in the model?
Thanks.
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: How to use CheckboxFor to set checked property
Jan 30, 2011 05:42 PM|LINK
You have far too much complexity to solve the problem. Create the simplest possible sample that reproduces your problem.
Jonathan Che...
Member
525 Points
97 Posts
Re: How to use CheckboxFor to set checked property
Jan 31, 2011 05:18 AM|LINK
Hi Caofanqc,
I tried the follow sample code, it works to initialize the check box. I use MVC 3.0, you can convert it to mvc 2.0:
Model view:
public class Test { public bool Sunday { get; set; } public bool Monday { get; set; } public bool AllowSunday { get; set; } public bool AllowMonday { get; set; } }Action:
public ActionResult search() { t.Monday = true; t.AllowMonday = true; t.Sunday = false; t.AllowSunday = false; return View(t); } [HttpPost] public ActionResult search(Test m) { return RedirectToAction("search"); }View:@using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Test</legend> <div class="editor-label"> @Html.LabelFor(model => model.Monday) @Html.CheckBoxFor(model => model.Monday, new { @checked = Model.AllowMonday}) </div> <div class="editor-label"> @Html.LabelFor(model => model.Sunday) @Html.CheckBoxFor(model => model.Sunday, new { @checked = Model.AllowSunday }) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> }Mvc 2.0
caofangc
Member
5 Points
35 Posts
Re: How to use CheckboxFor to set checked property
Jan 31, 2011 06:49 PM|LINK
I followed your approach but it doesn't work. Can you post the resulting Html?
Jonathan Che...
Member
525 Points
97 Posts
Re: How to use CheckboxFor to set checked property
Feb 01, 2011 01:06 AM|LINK
Hi Cao,
The version I used is MVC 3.0, but I think 2.0 is also OK. The follow is the result Html.
<form action="/" method="post"> <fieldset> <legend>Test</legend> <div class="editor-label"> <label for="Monday">Monday</label> <input checked="checked" data-val="true" data-val-required="The Monday field is required." id="Monday" name="Monday" type="checkbox" value="true" /> <input name="Monday" type="hidden" value="false" /> </div> <div class="editor-label"> <label for="Sunday">Sunday</label> <input data-val="true" data-val-required="The Sunday field is required." id="Sunday" name="Sunday" type="checkbox" value="true" /> <input name="Sunday" type="hidden" value="false" /> </div> <p> <input type="submit" value="Save" /> </p> </fieldset> </form>