Webgrid 中的 Mvc 3 texbox(剃刀) [英] Mvc 3 texbox in webgrid (razor)

查看:22
本文介绍了Webgrid 中的 Mvc 3 texbox(剃刀)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单问题:我如何让文本框显示值.下面的代码在 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: &lt;input type="text" name="LastName" value="@item.LastName" /&gt; // 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屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆