我在 Strongly Typed Html Helpers 上阅读了 ScottGu 的解释我知道它使我能够更好地对 View 进行编译时检查。当我在 MVC1 Html.TextBox 帮助器中使用 model.PropertyName 时,我的印象是我已经有了这个,但显然这不是真的。那么,使用 lambda 表达式如何为我做得更好呢?
最佳答案
考虑现有 HTML 辅助方法的语法:
<%= Html.TextBox("Quantity", Model.Quantity) %>
如果重命名
Quantity
将对象上的属性设置为“CurrentQuantity”,生成 <input>
元素仍将有 name="Quantity"
指定,如果您不记得更改第一个参数,模型绑定(bind)将中断。通过使用 lambda 表达式来指定元素的名称,不正确或拼写错误的属性名称将成为编译错误。
<!-- No magic strings here! -->
<%= Html.TextBoxFor(model => model.CurrentQuantity) %>
关于asp.net-mvc - 为什么强类型的 Html 助手可以帮助我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2045553/