MVC 2 asp.net VC2010:“数据绑定”在哪里发生?
理解问题:
1.)“X_IndexViewModel”类
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.)继承“X_IndexViewModel”的网站“Index.aspx”
<%@ 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)“真实”模型类“XModel”仅包含一个布尔值来存储是否存在的信息用户是否选择了复选框...
public class XModel
{
public bool CheckBox_1 {get; set;}
...
}
5) 在“XController.cs”中,
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(XModel model, FormCollection Form)
令我惊讶的是 POST ActionResult 方法的参数 model
很好地填充了 true
或 false
作为复选框。
我不明白的是:
我使用 X_IndexViewModel
类的实例 (X_IndexViewModel viewModel = new X_IndexViewModel { ... }
) 用一些值填充复选框(例如 selected = true 或 false 等)。
然后是使用 viewModel 呈现的网站(其中 viewModel 是 X_IndexViewModel 类的实例)。 这工作正常,因为网站继承了 X_IndexViwModel
。
当用户提交表单时,此事件(post 事件)由 [AcceptVerbs(HttpVerbs.Post)]
ActionResult
方法和 XModel
类属性由复选框值填充。
所以我的问题是:
“X_IndexViewModel”和“XModel”之间的绑定发生在哪里?
哪个语句说:X_IndexViewModel
CheckBox1
的返回值应存储在 XModel
属性 CheckBox1
中?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量编辑和重新阅读您的帖子后,我想我明白您的要求。
X_IndexViewModel
绑定到您创建的CheckBox
的原因是CheckBox
的命名与模型属性完全相同。它们都被命名为CheckBox1
。这就是 ASP.NET MVC 魔法发生的地方。它将模型属性与名称匹配的值进行匹配,并且可以加载这些值。
因此,当发布控件时,会发生相反的情况。使用不同的类来接收数据并不重要,因为它匹配属性名称和预期类型。由于数据被发布到一个控制器,该控制器的模型具有与 HTML 中的
CheckBox
控件同名的属性,因此它会自动将值放入其中。这一切都在幕后发生MVC 框架,我喜欢称其为MVC MAGIC SAUCE。如果您想测试它,请使用您的确切示例并将
XModel
bool
属性名称更改为CheckBox2
。这些值不会自动放入接收模型中,因为属性名称不再与 HTML 的控件名称 (ID) 匹配。您也可以对传入的原始模型执行相同的操作来创建视图 (
X_IndexViewModel
)。将其更改为CheckBox2
并且 HTML 将不再自动反映模型中的值,因为CheckBox2
的模型属性与CheckBox1< 的控件名称不匹配/代码>。
您可以阅读以下一些其他链接,其中包含更多示例和说明:
After much editting and re-reading your post I think I understand what you are asking. The reason your
X_IndexViewModel
binds to theCheckBox
you created is because theCheckBox
is named the exact same as the model property. They are both namedCheckBox1
.This is where th ASP.NET MVC magic happens. It matches up the models properties to the values where the names match up and it is possible to load the values.
So when the controls are posted, the reverse happens. It doesn't matter that a different class is being used to receive the data since it is matching up the property names and expected types. Since the data is being posted to a controller that has a model with a property of the same name as the
CheckBox
control in the HTML then it automatically puts the value in. This all happens behind the scenes by the MVC framework and I like to call it the MVC MAGIC SAUCE.If you want to test it, take your exact example and change the
XModel
bool
property name toCheckBox2
. The values will not automatically get thrown into the receiving model because the property name no longer matches the HTML's control name (ID).You could also do the same with the original model you pass in to create the view (
X_IndexViewModel
) as well. Change it toCheckBox2
and the HTML will no longer automatically reflect the value in the model since the model property ofCheckBox2
does not match the contols name which isCheckBox1
.Here are some other links you can read that have more examples and explanations: