How do I render a group of checkboxes using MVC 4 and View Models (strongly typed)

mahar shoaib

New Member
I'm rather new to the ASP.net MVC world and I'm trying to figure out how to render a group of checkboxes that are strongly typed to a view model. In webforms I would just use the checkboxlist control but im a bit lost with MVC.I'm building a simple contact form for a wedding planning business and need to pass whatever checkbox values the user selects to my controller.The form checkboxes need to look like this:
hWwLQ.gif
Your help would be greatly appreciated. Thanks!Here's what I have so far.CONTROLLER\[code\][HttpPost]public ActionResult Contact(ContactViewModel ContactVM){ if (!ModelState.IsValid) { return View(ContactVM); } else { //Send email logic return RedirectToAction("ContactConfirm"); }}\[/code\]VIEW MODEL\[code\]public class ContactViewModel{ [Required] public string Name { get; set; } [Required] public string Phone { get; set; } [Required] [DataType(DataType.EmailAddress)] public string Email { get; set; } [Required] public string Subject { get; set; } public IEnumerable<SelectListItem> SubjectValues { get { return new[] { new SelectListItem { Value = "http://stackoverflow.com/questions/12808936/General Inquiry", Text = "General Inquiry" }, new SelectListItem { Value = "http://stackoverflow.com/questions/12808936/Full Wedding Package", Text = "Full Wedding Package" }, new SelectListItem { Value = "http://stackoverflow.com/questions/12808936/Day of Wedding", Text = "Day of Wedding" }, new SelectListItem { Value = "http://stackoverflow.com/questions/12808936/Hourly Consultation", Text = "Hourly Consultation" } }; } } //Not sure what I should do for checkboxes...}\[/code\]VIEW\[code\]@model NBP.ViewModels.ContactViewModel@{ ViewBag.Title = "Contact"; Layout = "~/Views/Shared/_Layout.cshtml";}@using (Html.BeginForm()){ <div id="ContactContainer"> <div><span class="RequiredField">*&nbsp;</span>Your Name:</div> <div> @Html.TextBoxFor(model => model.Name) </div> <div><span class="RequiredField">*&nbsp;</span>Your Phone:</div> <div> @Html.TextBoxFor(model => model.Phone) </div> <div><span class="RequiredField">*&nbsp;</span>Your Email:</div> <div> @Html.TextBoxFor(model => model.Email) </div> <div>Subject:</div> <div> @Html.DropDownListFor(model => model.Subject, Model.SubjectValues) </div> <div>Vendor Assistance:</div> <div> <!-- CHECKBOXES HERE --> </div> <div> <input id="btnSubmit" type="submit" value="http://stackoverflow.com/questions/12808936/Submit" /> </div> </div>}\[/code\]
 
Back
Top