在 WebGrid.column 内的 HTML.ActionLink 中使用数据,不可能吗?

发布于 2024-11-10 01:36:50 字数 1012 浏览 0 评论 0原文

我的 ASP.NET MVC3 测试应用程序中有以下 WebGrid。它显示客户列表:

@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns
         (
         grid.Column(format: (item) => Html.ActionLink("Edit", "Details", new { id = item.id })),
         grid.Column("Address.CompanyName"),
         grid.Column("Address.City")
         )
)

这里有趣的部分是我在第一列中添加的编辑链接。我想使用客户帐号而不是简单的“编辑”测试。然而,这样做给我带来了很多问题。

我已经尝试过:

grid.Column(format: (item) => Html.ActionLink(item.AccountNumber.ToString(), "Details", new { id = item.id })),

但是,似乎有一些我不明白这是如何工作的,因为我不断收到此异常:

CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func<dynamic,object>, string, bool)' has some invalid arguments

任何人都可以向我解释为什么这不起作用吗? “Edit”和 item.AccountNumber.ToString() 之间有什么区别(除了拼写)?

我应该注意到,使用“编辑”文本时该链接有效,并且 AccountNumber 很长。

I have the following WebGrid in my ASP.NET MVC3 test application. It displays a list of customers:

@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns
         (
         grid.Column(format: (item) => Html.ActionLink("Edit", "Details", new { id = item.id })),
         grid.Column("Address.CompanyName"),
         grid.Column("Address.City")
         )
)

The interesting part here is the Edit-link I've added in the first column. I would like to use the customers account number instead of the plain "Edit"-test. However, it causes me a great deal of problems to do so.

I've tried:

grid.Column(format: (item) => Html.ActionLink(item.AccountNumber.ToString(), "Details", new { id = item.id })),

However, it seems like there is something i don't understand about how this works because i keep getting this exception:

CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func<dynamic,object>, string, bool)' has some invalid arguments

Can anyone explain to me why this isn't working? What is the difference between "Edit" and item.AccountNumber.ToString() (apart from the spelling)?

I should note that the link works when using the "Edit"-text, and that AccountNumber is a long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

忆梦 2024-11-17 01:36:50

这是我如何处理日期的示例。

grid.Column(columnName: "Date", format: (item) => Html.ActionLink(((string)item.Date), "Edit", new { id = item.id })),          

使用扩展程序时必须小心
具有动态性的方法 (Html.*)
(项目)...它在以下方面效果不佳
csharp。当您执行新的 {} 操作时
投影或调用 ToString,它不是
更长的动态。或者,你
可以投射:(object)item.Id。

此处

Here is an example of how I do it with a date.

grid.Column(columnName: "Date", format: (item) => Html.ActionLink(((string)item.Date), "Edit", new { id = item.id })),          

You have to beware of using extension
methods (Html.*) with dynamics
(item)... it doesn't work well in
csharp. When you do the new {}
projection or call ToString, it's no
longer dynamic. Alternatively, you
could cast: (object)item.Id.

From here.

寻找我们的幸福 2024-11-17 01:36:50

以防万一有人想知道它在 VB 中应该是什么样子,这里有一个例子:

grid.Column("PersonID", "Admin", Function(modelItem) Html.ActionLink("View", "Details", New With {.id = modelItem.PersonID}))

Just in case any one was wondering about how it should look in VB here is an example:

grid.Column("PersonID", "Admin", Function(modelItem) Html.ActionLink("View", "Details", New With {.id = modelItem.PersonID}))
神爱温柔 2024-11-17 01:36:50

就我而言,Derek Beattie 解决方案不起作用。

我用这个

 grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID }), style: "column-action") 

In my case Derek Beattie solution is not working.

And I use this

 grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID }), style: "column-action") 
刘备忘录 2024-11-17 01:36:50

我的列是在模型中生成的,其中 Html.ActionLink 似乎无法访问。所以我必须创建 href 并将其作为 MvcHtmlString 返回。这就是我最终所做的,

new WebGridColumn{ColumnName="FileName", Header= "File",
                Format = item => new MvcHtmlString("<a href='" + item.FileLink + "'>" + item.FileName +"</a>")

My columns were being generated in the Model, where Html.ActionLink seems inaccessible. So I had to create the href and return it as an MvcHtmlString. This is what I ended up doing,

new WebGridColumn{ColumnName="FileName", Header= "File",
                Format = item => new MvcHtmlString("<a href='" + item.FileLink + "'>" + item.FileName +"</a>")
情域 2024-11-17 01:36:50

我已经这样解决了

grid.Column("Id", format: (item) => Html.ActionLink((string)item.id.ToString(), "Edit", new { id = item.id }))

I have solved like this

grid.Column("Id", format: (item) => Html.ActionLink((string)item.id.ToString(), "Edit", new { id = item.id }))

羁绊已千年 2024-11-17 01:36:50

我对网格使用以下代码。它对我有用。

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column(header: "Serial No", format:@<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
        grid.Column(columnName: "Stdname", header: "Student Name"),
        grid.Column(header: "Email ID", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>),
        grid.Column(header: "EDIT",format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID })),
        grid.Column(header: "DETAILS", format: (item) => Html.ActionLink("Details", "Details", new { id = item.ID })),
        grid.Column(header: "DELETE", format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.ID }))
))

希望这有帮助。

I use following code for grid. It working for me.

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column(header: "Serial No", format:@<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
        grid.Column(columnName: "Stdname", header: "Student Name"),
        grid.Column(header: "Email ID", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>),
        grid.Column(header: "EDIT",format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID })),
        grid.Column(header: "DETAILS", format: (item) => Html.ActionLink("Details", "Details", new { id = item.ID })),
        grid.Column(header: "DELETE", format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.ID }))
))

Hope this is helpful.

如梦 2024-11-17 01:36:50
 @Html.Grid(Model).Columns(columns => {
    columns.Add(c => c.ConsumerNo).Titled("Consumer No").SetWidth(70).Filterable(true);
    columns.Add(c => c.ConsumerName).Titled("Consumer Name").SetWidth(200).Filterable(true);
    columns.Add(c => c.MobileNo).Titled("Mobile No").SetWidth(70).Filterable(true);
    columns.Add(c => c.Address).Titled("Address").SetWidth(200).Filterable(true);
    columns.Add(c => c.AreaName).Titled("Area Name").SetWidth(70).Filterable(true);
    columns.Add(c => c.StaffName).Titled("Staff Name").SetWidth(100).Filterable(true);
    columns.Add().Encoded(false).Sanitized(false).Titled("INSPECT").SetWidth(60).RenderValueAs(o => Html.ActionLink("INSPECT", "InspForm", new { id = o.UniqueConsumerId, style = "background-image:url('~/Images/orderedList1.png')" }));                       
}).WithPaging(10).Sortable(true)
 @Html.Grid(Model).Columns(columns => {
    columns.Add(c => c.ConsumerNo).Titled("Consumer No").SetWidth(70).Filterable(true);
    columns.Add(c => c.ConsumerName).Titled("Consumer Name").SetWidth(200).Filterable(true);
    columns.Add(c => c.MobileNo).Titled("Mobile No").SetWidth(70).Filterable(true);
    columns.Add(c => c.Address).Titled("Address").SetWidth(200).Filterable(true);
    columns.Add(c => c.AreaName).Titled("Area Name").SetWidth(70).Filterable(true);
    columns.Add(c => c.StaffName).Titled("Staff Name").SetWidth(100).Filterable(true);
    columns.Add().Encoded(false).Sanitized(false).Titled("INSPECT").SetWidth(60).RenderValueAs(o => Html.ActionLink("INSPECT", "InspForm", new { id = o.UniqueConsumerId, style = "background-image:url('~/Images/orderedList1.png')" }));                       
}).WithPaging(10).Sortable(true)
天赋异禀 2024-11-17 01:36:50
 grid.Column("GiftID",canSort:false, format: (item => Html.ActionLink((string)(@item.GiftID).ToString(), "Edit", new { GiftID = @item.GiftID })))
 grid.Column("GiftID",canSort:false, format: (item => Html.ActionLink((string)(@item.GiftID).ToString(), "Edit", new { GiftID = @item.GiftID })))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文