Understanding question:
1.) "X_IndexViewModel" class
public class X_IndexViewModel
{
public List<SelectListItem> CheckBox_1 { get; set; }
...
}
2.) XController.cs
public ActionResult Index()
{
X_IndexViewModel viewModel = new X_IndexViewModel
{
CheckBox_1 = new List<SelectListItem>()
{
new SelectListItem
{
Selected = true,
Text = "some text",
Value ="another text"
}
},
...
}
return View(viewModel);
}
3.) Website "Index.aspx" which inherits "X_IndexViewModel"
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Test.ViewModels.X_IndexViewModel>" %>
...
<!-- rendering a checkbox -->
<% foreach (var item in Model.CheckBox_1) { %>
<%: Html.CheckBox("CheckBox_1", item.Selected, new {id="CheckBox_1"}) %>
<label for="CheckBox_1<%: item.Text %>"><%: item.Text %></label>
<% } %>
...
4) The "real" model class "XModel" contains just a bool to store the information whether the user selected the checkbox or not...
public class XModel
{
public bool CheckBox_1 {get; set;}
...
}
5) And in "XController.cs"
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(XModel model, FormCollection Form)
It surprises me that the argument model
of the POST ActionResult method is well filled with true
or false
for the checkbox.
What I didn't understand:
I use an instance of the X_IndexViewModel
class
( X_IndexViewModel viewModel = new X_IndexViewModel { ... }
)
to fill the checkbox with some values (like selected = true or false, etc.).
Then is the website rendered by using the viewModel (where viewModel is an instance of the X_IndexViewModel class).
This works fine because the website inherits X_IndexViwModel
.
When the user submits the form is this event (the post event) fetched by the [AcceptVerbs(HttpVerbs.Post)]
ActionResult
method AND the XModel
class properties are filled with the checkbox value.
So my question is:
Where does the binding between the "X_IndexViewModel" and the "XModel" happen?
Which statement says: The returned value of the X_IndexViewModel
CheckBox1
should be stored in the XModel
property CheckBox1
?