I have a Fairly Complex Form that I need to post to my MVC controller.
Here is the View model which I initially pass to the view on creation:
public class EditViewModel
{
public Service service { get; set; }
public bool sms { get; set; }
public bool email { get; set; }
public string userId { get; set; }
}
Here is my View (simplified):
@model IList<Service_Monitor_Web_Interface.Models.ViewModels.EditViewModel>
@{
ViewBag.Title = "Configure User Notifications";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("Edit", "Users", FormMethod.Post, new { @class = "stdform stdform2", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
<p>
<label><u> Service:</u> </label>
<span class="field">
<u>Notification Methods:</u>
</span>
</p>
for (int i = 0; i < Model.Count; i++)
{
<p>
<label>@Model[i].service.Name</label>
<span class="field">
@Html.CheckBoxFor(model => model[i].sms)
SMS
@Html.CheckBoxFor(model => model[i].email)
Email
</span>
</p>
}
<br clear="all" /><br />
<p class="stdformbutton">
<button class="submit radius2">Save</button>
<input type="reset" class="reset radius2" value="Reset Form" />
</p>
}
And here is my Action method in my controller:
//
// POST: /Users/Edit
[HttpPost]
public ActionResult Edit(IList<EditViewModel> viewModel)
{
return View(viewModel);
}
How Can I bind my view model when receiving it on the controller? Currently when I debug the action method receives a ViewModel which looks like so:
How can I get service and userId not to be null?