开发者

lambda expression meaning

开发者 https://www.devze.com 2023-04-13 06:14 出处:网络
I have created an htmlhelper which has signature like this: public static MvcHtmlString PageLinks(this HtmlHelper html,

I have created an htmlhelper which has signature like this:

 public static MvcHtmlString PageLinks(this HtmlHelper html,
     PagingInfo pagingInfo,Func<int, string> pageUrl)
开发者_高级运维

and I see that It is passed like this:

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))

but I dont know what is it meaning ?

x => Url.Action("List", new {page = x})

Please suggest meaning of this lambda expression


Well, it's a lambda expression which takes a single parameter called x and is implicitly typed to be a int, and which calls Url.Action with one argument of the string "List" another argument of an anonymous type which has a single property page with the value of the parameter x.

The value returned from Url.Action is used as the return value for the lambda expression.

That's how the lambda expression itself is broken down, in terms of language constructs. If you're asking what it means in terms of behaviour, that's a different matter - you'd need to look at your own PageLinks method, as well as the documentation for Url.Action.


x => Url.Action("List", new {page = x})

This is a (lambda) function that given an x (and we know that x must be integer because the signature is Func<int, string>) returns an url (the string). (technically it is a lambda expression, but the difference is lost in this particular case)

You use it like this: string url = pageUrl(pageNumber);

Url.Action gives the url of the action List that has a single parameter (of type int and name page probably) that will be filled with the value of x. This parameter will probably be the page of the list that must be returned.

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))

This will "return" an html string with a link to the url we calculated before. (probably an <a href='url'></a> or something similar) ah... it's your function :-)

(I think you are doing something like this: ASP.NET MVC Paging, Preserving Current Query Parameters with the PagingInfo class containing the current and total pages, right?)

0

精彩评论

暂无评论...
验证码 换一张
取 消