質問

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

I have this in my view and I want to add 2 more properties (so I added some more arguments to the Func delegate...):

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List",
    new { page = x, 
          sort = Request.QueryString["sort"], 
          desc = Request.QueryString["desc"] }))

but when I add 2 more properties it doesn't work... what am I not understanding here?

And I have the following html helper:

public static MvcHtmlString PageLinks(this HtmlHelper html,
    PagingInfo pagingInfo, Func<int, string, string, string> pageUrl)
{
    StringBuilder result = new StringBuilder();
    for (int i = 1; i <= pagingInfo.TotalPages; i++)
    {
        TagBuilder liTag = new TagBuilder("li"); 
        TagBuilder aTag = new TagBuilder("a"); 

        aTag.MergeAttribute("href", pageUrl(i, 
           pagingInfo.Sort, pagingInfo.Desc.ToString())); // pageUrl delegate is here helloooo
        aTag.InnerHtml = i.ToString();
        if (i == pagingInfo.CurrentPage)
            liTag.AddCssClass("active");

        liTag.InnerHtml = aTag.ToString();

        result.Append(liTag.ToString());
    }
    return MvcHtmlString.Create(result.ToString());
}

P.S my controller looks like this:

public ViewResult List(int page = 1, string sort = "Name", bool desc = false)
{...
役に立ちましたか?

解決

The

x => Url.Action("List",
   new { page = x, 
      sort = Request.QueryString["sort"], 
      desc = Request.QueryString["desc"] })

is still a lambda expression that denotes a method that expects one parameter. The fact that in the method body you create an anonymous object with three properties is irrelevant.

You want to change it to

(x, y, z) => Url.Action("List",
   new { page = x, 
      sort = y, 
      desc = z }))

This way when you invoke it as

pageUrl(i, pagingInfo.Sort, pagingInfo.Desc.ToString())

all three ( i, pagingInfo.Sort and paginginInfo.Desc.ToString()) will be captured by consecutive parameters (x, y and z). I would even recommend renaming for clarity:

(pageNumber, sortOrder, descending) => Url.Action("List",
   new { page = pageNumber, sort = sortOrder, desc = descending }))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top