Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - Using data in a HTML.ActionLink inside a WebGrid.column, not possible?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...