Webgrid 中的 Mvc 3 texbox(剃刀)
简单问:如何让文本框显示值。下面的代码在 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
扩展方法(即 Html.TextBox)不能很好地与动态对象(即项目)一起工作...这是 c# 的限制。
您有几个选项:
格式:
InputExtensions.TextBox(Html, "Last Name", item.LastName) // 静态调用
格式:
Html.TextBox("Last Name", (object)item.LastName) // 转换为非动态对象
格式:
> // 避免扩展
另外,我相信有一个带有“item”参数的固有 lambda - 您不需要自己声明它。
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.
相当复杂但有效:
也许有更好的方法。仍在学习 Razor 语法,坦率地说,在使用 MVCContrib 网格。
Quite convoluted but works:
Maybe there's a better approach though. Still learning the Razor syntax and quite frankly I am a bit disappointed by the WebGrid helper after having used MVCContrib Grid.
那个对我有用
That one works for me
试试这个:
Try this:
试试这个
Try this
我必须将对象转换为字符串,如下所示:
I had to cast object to string as follows:
根据条件显示链接或纯文本:
Showing Link or plain text based upon a condition: