将Lambda表达式用于分页MVC [英] Using Lambda Expression for Pagination MVC

查看:193
本文介绍了将Lambda表达式用于分页MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在学习MVC并且正在效仿。我也不是Lambda表达式和匿名类型的专家,尽管我已经阅读并了解了基础知识。然而,我被困,无法继续。我需要有关此代码段的帮助。有人可以回答我的问题吗?



1.如何推断X是INT?



2.new {page = x}的回报是什么?



3.Url.Action(List,new的回报是什么? {page = x})?



4.x => Url.Action(List,新{page = x}的回报是什么)?



5.此调用,pageUrl(i),它做了什么以及声明的方法在哪里?





 <   div     class   =  pager >  
@ Html.PageLinks(Model.PagingInfo,x => Url.Action (列表,新{page = x}))
< / div < span class =code-keyword>>







  public   static  MvcHtmlString PageLinks( this  HtmlHelper html,PagingInfo pagingInfo,Func< ; int,string> pageUrl)
{
StringBuilder result = new StringBuilder();
for int i = 1 ; i < = pagingInfo.TotalPages; i ++)
{
TagBuilder tag = new TagBuilder( a); // 构建< a> tag
tag.MergeAttribute( href,pageUrl(i)) ;
tag.InnerHtml = i.ToString();

if (i == pagingInfo.CurrentPage)
tag.AddCssClass( 选中);

result.Append(tag.ToString());
}

return MvcHtmlString.Create(result.ToString());

}



非常感谢。

解决方案

Html.PageLinks(...)是用于分页的自定义HTML帮助程序的调用。 HTML Helper类返回用于构建HTML标记链接的字符串。 PagingInfo模型使用此静态助手类。每次用户单击页码时,都会将其作为currentPage参数值传递给PagingInfo。此值将用作X.



Url.Action(List,new {page = x})使用当前控制器的给定操作名称(List)和路径的给定参数( new {创建URL page = x} ),结果将是这样的: YourCurrentController \List \?page = 5 ,假设用户点击了第5页(X将与5匹配)。


1)它取决于什么是X和使用推理的代码。您应该了解主要内容:推断与匿名类型完全无关。



2-4)执行以下操作。将显示的表达式分配给某个变量。在调试器下,在赋值后停止,并检查此变量。比如,这是X,检查: X X.GetType()。你将得到所有答案。



5)该方法在你的第二个代码片段中声明。 :-)

第一个代码片段调用此方法并将返回的字符串(计算结果是一些HTML字符串)插入到作为HTTP响应返回的内容中。



-SA


不明白为什么Url.Action(List,new {page = 1})返回/?页= 1

Hi everyone,

I am studying MVC and was following an example. I am also not expert with Lambda expressions and ANonymous types, although I have read and know the basics. However I got trapped and could not continue. I need help about this code snippets below. Can someone please answer my question ?

1. how the X was inferred to be an INT ?

2. What is the return of "new { page = x }" ?

3. What is the return of "Url.Action("List", new { page = x })" ?

4. What is the return of "x => Url.Action("List", new { page = x })" ?

5. This call, pageUrl(i), what does it do and where is the method declared ?


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




 public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
 {
             StringBuilder result = new StringBuilder();
             for (int i = 1; i <= pagingInfo.TotalPages; i++)
             {
                 TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
                 tag.MergeAttribute("href", pageUrl(i));
                 tag.InnerHtml = i.ToString();

                if (i == pagingInfo.CurrentPage)
                     tag.AddCssClass("selected");

                result.Append(tag.ToString());
             }

            return MvcHtmlString.Create(result.ToString());

}


Thank you so much.

解决方案

Html.PageLinks(...) is the invocation of your custom HTML Helper used for pagination. The HTML Helper class returns a string for building HTML tag link. This static helper class is used by PagingInfo model. Each time a user clicks the page number, it is passed to PagingInfo as currentPage parameter value. And this value will be used as X.

Url.Action("List", new { page = x }) create an URL by using the given action name ("List") of the current controller and the given parameter for the route ( new { page = x }), so the result will be something like this: "YourCurrentController\List\?page=5", supposing that the user have clicked on 5th page (and X will be matched with 5).


1) It depends on what is X and the code using inference. You should understand the main thing: inference is totally unrelated to anonymous types.

2-4) Do the following. Assign the expressions you show to some variable. Under the debugger, stop after assignment, and inspect this variable. Say, of this is X, inspect: X and X.GetType(). You will get all the answers.

5) The method is declared in your second code fragment. :-)
The first code fragments calls this methods and inserts the string returned (the result of the calculation is some HTML string) into the content returned as HTTP response.

—SA


Do not understand why Url.Action("List", new { page = 1 })" , returns "/?page=1"


这篇关于将Lambda表达式用于分页MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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