Webgrid 中的 Mvc 3 texbox(剃刀) [英] Mvc 3 texbox in webgrid (razor)
问题描述
简单问题:我如何让文本框显示值.下面的代码在 item.LastName 上失败
@model List@{var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);}<div id="grid1">@grid.GetHtml(列:grid.Columns(grid.Column("姓氏"),grid.Column(格式: (item) => Html.TextBox("LastName", item.LastName))))
扩展方法(即 Html.TextBox)不适用于动态对象(即 item)...这是 c# 的限制.
您有几个选择:
格式:InputExtensions.TextBox(Html, "Last Name", item.LastName)//静态调用
format: Html.TextBox("Last Name", (object)item.LastName)//强制转换为非动态对象
格式:<input type="text" name="LastName" value="@item.LastName"/>//避免扩展
此外,我相信有一个带有item"参数的固有 lambda - 您不需要自己声明它.
Simple Q:How do you I get the textbox to show the value. Code below fail on item.LastName
@model List<Mvc2010_11_12.Models.Employee>
@{
var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);
}
<div id="grid1">
@grid.GetHtml(columns: grid.Columns(
grid.Column("LastName"),
grid.Column(format: (item) => Html.TextBox("LastName", item.LastName))
))
</div>
Extension methods (i.e., Html.TextBox) don't work well with dynamic objects (i.e., item)... it's a limitation of c#.
You've got a few options:
format: InputExtensions.TextBox(Html, "Last Name", item.LastName) // static call
format: Html.TextBox("Last Name", (object)item.LastName) // cast as non-dynamic object
format: <input type="text" name="LastName" value="@item.LastName" /> // avoid extensions
Also, I believe there's an inherent lambda with an "item" parameter - you shouldn't need to declare this yourself.
这篇关于Webgrid 中的 Mvc 3 texbox(剃刀)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!