Asp.net mvc 2 模型绑定器幕后故事

发布于 2024-09-16 15:28:52 字数 774 浏览 10 评论 0原文

我从 MVC2 开始,有一个简单的问题:

如果我有一个内部带有表单的类型化视图,并且这个文本框是用 lambda 表达式创建的:

 <%: Html.TextBoxFor(e => e.Name)%>

当我提交此表单时,默认模型绑定程序将采用请求表单,将模型输入到查看、序列化发布的数据(作为此模型)并将其传递给我的控制器的操作。

为了更好地解释自己,让我们假设我有一个像 localhost/edittestmodel/ID/1 这样的 url,并且在我的控制器操作中有以下代码:

public ActionResult Edit(int id)
{
    TestModel testmodel=new TestModel();
    testmodel.Name="texttorenderintotextbox";
    //whats the class that place the testmodel properties into the view? 
    return View(testmodel);

}

What's the responsable class for place the Name property of my testmodel object into the textbox

<%: Html.TextBoxFor(e => e.Name)%>

谢谢提前。

此致。

何塞.

Im starting with MVC2 and i have a simple question:

If i have a typed view with a form inside, and this textbox created with lambda expressions:

 <%: Html.TextBoxFor(e => e.Name)%>

When i submit this form the default model binder take the request form, takes the model typed to the view, serialize the data posted (as a this model) and pass it to an action of my controller.

To try to explain myself better, lets imagine that i have a url like localhost/edittestmodel/ID/1 and i have in my controller action the following code:

public ActionResult Edit(int id)
{
    TestModel testmodel=new TestModel();
    testmodel.Name="texttorenderintotextbox";
    //whats the class that place the testmodel properties into the view? 
    return View(testmodel);

}

What's the responsable class for place the Name property of my testmodel object into the textbox

<%: Html.TextBoxFor(e => e.Name)%>

Thanks in advance.

Best Regards.

Jose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够 2024-09-23 15:28:59

TextBoxFor 帮助程序方法负责从拉姆达表达式。

It's the TextBoxFor helper method that's responsible for generating the input field from the lambda expression.

陌伤ぢ 2024-09-23 15:28:59

视图在 POST 请求和模型绑定中没有任何关系

当您拥有强类型视图时,模型类型几乎不用于在视图代码中实现代码智能感知的简单性(因此您可以像示例中那样使用 lambda 表达式)。

但是,当您将数据发回时,不会根据视图检查任何内容。不过,它会根据控制器操作参数进行检查。如果存在具有特定自定义模型绑定程序类型的参数,则该特定模型绑定程序用于处理传入数据。

但是要回答您的问题TextBoxFor 检查您的强类型视图的模型并生成具有正确名称属性的特定文本框。因此,其中的数据将在正确的表单字段名称下发回。

更进一步。它是视图引擎,解析视图的 ASPX 代码并运行所有服务器端脚本,包括 Html.TextBoxFor() 调用。

View's don't have anything to do in POST request and model binding

When you have strong type views, model type is barely used to have the simplicity of code intellisense in view's code (so you can use lambda expressions like in your example).

But when you POST data back, nothing gets checked against the view. It gets checked against controller action parameters though. And if there's a parameter with a certain custom model binder type, that particular model binder is used to process incoming data.

But to answer your question: TextBoxFor checks your strong type view's model and generates particular textbox with correct name attribute. So data from it will be posted back under the correct form field name.

To go even deeper. It's the view engine that parses view's ASPX code and runs all server side scripts including Html.TextBoxFor() call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文