Webgrid 中的 Mvc 3 texbox(剃刀)

发布于 2024-10-02 22:44:44 字数 408 浏览 2 评论 0原文

简单问:如何让文本框显示值。下面的代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安稳善良 2024-10-09 22:44:44

扩展方法(即 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.

回眸一遍 2024-10-09 22:44:44

相当复杂但有效:

@helper TextField(Employee employee, HtmlHelper<IEnumerable<Employee>> html)
{
    @html.TextBoxFor(x => employee.LastName)
}

<div id="grid1">
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("LastName"),
        grid.Column(format: item => TextField(item.Value, Html))
    ))
</div>

也许有更好的方法。仍在学习 Razor 语法,坦率地说,在使用 MVCContrib 网格

Quite convoluted but works:

@helper TextField(Employee employee, HtmlHelper<IEnumerable<Employee>> html)
{
    @html.TextBoxFor(x => employee.LastName)
}

<div id="grid1">
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("LastName"),
        grid.Column(format: item => TextField(item.Value, Html))
    ))
</div>

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.

余生再见 2024-10-09 22:44:44

那个对我有用

@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: @<span>@Html.TextBox("LastName",@item.LastName as object)</span>   )
    ))
</div>

That one works for me

@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: @<span>@Html.TextBox("LastName",@item.LastName as object)</span>   )
    ))
</div>
往事风中埋 2024-10-09 22:44:44

试试这个:

grid.Column(format: (item) => Html.TextBox("LastName", (object) item.LastName))

Try this:

grid.Column(format: (item) => Html.TextBox("LastName", (object) item.LastName))
仙气飘飘 2024-10-09 22:44:44

试试这个

grid.Column("AmountValue", header: "Amount", format:@<text><input name="Amount" type="text" value="@item.AmountValue"</text>)

Try this

grid.Column("AmountValue", header: "Amount", format:@<text><input name="Amount" type="text" value="@item.AmountValue"</text>)
感悟人生的甜 2024-10-09 22:44:44

我必须将对象转换为字符串,如下所示:

format: (item) => Html.ActionLink(
    (item.AppCode as object).ToString(),
    "Index",
    "ApplicationHealthCheck",
    new { Code = item.AppCode },
    null),

I had to cast object to string as follows:

format: (item) => Html.ActionLink(
    (item.AppCode as object).ToString(),
    "Index",
    "ApplicationHealthCheck",
    new { Code = item.AppCode },
    null),
青衫负雪 2024-10-09 22:44:44

根据条件显示链接或纯文本:

grid.Column(format: (dbItem) => dbItem.QtyCompleted == 0 ?
                 Html.ActionLink(
                 "Start Setup",
                 "SaveData", 
                 "Workorder",
                 new
                   {
                       msid = dbItem.MSID,
                       qtyCompleted = 0, 
                       serialNum = "ABC"
                   },
                 new 
                   { 
                       onclick = "return ConfirmAction('Start Setup');" 
                   }) :
                 Html.Label(((DateTime)dbItem.StartTime).ToShortDateString())
            )

Showing Link or plain text based upon a condition:

grid.Column(format: (dbItem) => dbItem.QtyCompleted == 0 ?
                 Html.ActionLink(
                 "Start Setup",
                 "SaveData", 
                 "Workorder",
                 new
                   {
                       msid = dbItem.MSID,
                       qtyCompleted = 0, 
                       serialNum = "ABC"
                   },
                 new 
                   { 
                       onclick = "return ConfirmAction('Start Setup');" 
                   }) :
                 Html.Label(((DateTime)dbItem.StartTime).ToShortDateString())
            )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文