开发者

ASP.NET MVC 2 RouteLink method

开发者 https://www.devze.com 2023-02-11 09:25 出处:网络
I\'m new to ASP.NET MVC and can\'t figure this out.Working through the Nerdinner example from Professional ASP.NET MVC 2, I copied the PaginatedList helper class and decided to improve it so the forwa

I'm new to ASP.NET MVC and can't figure this out. Working through the Nerdinner example from Professional ASP.NET MVC 2, I copied the PaginatedList helper class and decided to improve it so the forward and back links could be generated by a method within the class instead of writing them out in every view page. I copied this from the view that it was in, Index.aspx:

if (Model.HasPreviousPage)
    {
        Response.Write(Html.RouteLink("<<<", "Users", new { page=(Model.PageIndex-1) }));
    }

And used it to create this method within Helpers\PaginatedList.cs:

public string NavLinks()
{
    if (HasPreviousPage)
    {
        return Html.RouteLink("<<<", "Users", new { page=(PageIndex-1) });
    }
}

(HasPreviousPage is a simple method within PaginatedList.)

Straight away it complains that "The name 'Html' does not exist in the current context, so I modified it to take a parameter:

public string NavLinks(HtmlHelper Html)

Now I get "'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RouteLink' and no extension method 'RouteLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)".

According to Microsoft documentation on LinkExtensions.RouteLink method, "In Visual Basic and C#, you can call this method as an instance method on any ob开发者_JAVA技巧ject of type HtmlHelper". Do they lie?

Help!


If your trying to make this an HtmlHelper just change

public string NavLinks(HtmlHelper Html)
{
    if (HasPreviousPage)
    {
        return Html.RouteLink("<<<", "Users", new { page=(PageIndex-1) });
    }
}

to

using System.Web.Mvc;
using System.Web.Mvc.Html;

public static MvcHtmlString NavLinks(this HtmlHelper html, hasPreviousPage)
{
    if (hasPreviousPage)
    {
        return html.RouteLink("<<<", "Users", new { page=(PageIndex-1) });
    }
}

All of the HtmlHelpers contained within Mvc are static methods and also they return an MvcHtmlString in Asp.net MVC 2. This will be an extension method for the class HtmlHelper. After adding those references to your code file that contains this extension methods you should see the RouteLink method inside of there.


@DeathBedMotorcade

That helped, but I had to change a few things.

The method can't be static because it needs to access properties of the object (part of same class, PaginatedList), specifically the HasPreviousPage method and the PageIndex variable. Then it gave the error "Extension method must be static". Removing the "this" keyword fixed that. So I have:

public MvcHtmlString NavLinks(HtmlHelper html)
{
    if (HasPreviousPage)
    {
        return html.RouteLink("<<<", "Users", new { page=(PageIndex-1) });
    }
    if (HasNextPage)
    {
        return html.RouteLink(">>>", "Users", new { page=(PageIndex+1) });
    }
    return null;
} 

And in the view:

Response.Write(Model.NavLinks(Html))

And that works, though I don't fully understand all the reasons why it didn't before.

"using System.Web.Mvc.Html" was key although I thought I'd tried that at some point; probably before realising the item "Html" (an HtmlHelper object that is implicitly passed to the view?) needed to be passed in. VS usually prompts for missing Using statements but didn't this time.

Thanks.

0

精彩评论

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

关注公众号