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

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

问题描述

简单的问:你怎么我得到的文本框显示的值。 code以下无法在item.LastName

  @model名单< Mvc2010_11_12.Models.Employee>
@ {
    VAR电网=新的WebGrid(来源:型号,defaultSort:名字,rowsPerPage:3);
}< D​​IV ID =GRID1>
    @ grid.GetHtml(列:grid.Columns(
        grid.Column(姓氏),
        grid.Column(格式:(项目)=> Html.TextBox(姓氏,item.LastName))
    ))
< / DIV>


解决方案

扩展方法(即Html.TextBox)不能很好地与动态对象(即项目)......这是C#的限制。工作

您已经有了几个选择:

格式:InputExtensions.TextBox(HTML,姓,item.LastName)//静态调用

格式:Html.TextBox(姓,(对象)item.LastName)//投非动态对象

格式:LT;输入类型=文本名称=姓氏值=@ item.LastName/> //避免扩展

另外,我相信有以项参数的内在拉姆达 - 你不应该需要这个宣布自己

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.

这篇关于MVC 3 texbox中的WebGrid(剃刀)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆