English 中文(简体)
ASP.NET MVC 2 RouteLink方法
原标题:ASP.NET MVC 2 RouteLink method

我是ASP.NET MVC的新手,无法理解这一点。通过学习Professional ASP.NET MVC 2中的Nerminer示例,我复制了PaginatedList帮助程序类,并决定对其进行改进,以便可以通过类中的方法生成前向和后向链接,而不是在每个视图页中都写出它们。我是从Index.aspx中的视图中复制的:

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

并使用它在HelpersPaginatedList.cs中创建了此方法:

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

(HasPreviousPage是PaginatedList中的一个简单方法。)

它马上抱怨说“Html名称在当前上下文中不存在,所以我修改了它,使其采用一个参数:

public string NavLinks(HtmlHelper Html)

现在我得到“System.Web.Mvc.HtmlHelper不包含RouteLink的定义,并且找不到接受System.Web.Mvc.HtmlHelpers类型的第一个参数的扩展方法RouteLink(是否缺少using指令或程序集引用?)”。

根据Microsoft关于LinkExtensions.RouteLink方法的文档,“在Visual Basic和C#中,您可以将此方法作为HtmlHelper类型的任何对象的实例方法调用”。他们撒谎吗?

帮助

最佳回答

如果您试图将其作为HtmlHelper,请更改

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

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 到 your code file that contains this extension methods you should see the RouteLink method inside of there.

问题回答

@死亡之床摩托学员

这有帮助,但我不得不改变一些事情。

该方法不能是静态的,因为它需要访问对象(同一类的一部分,PaginatedList)的属性,特别是HasPreviousPage方法和PageIndex变量。然后它给出了错误“扩展方法必须是静态的”。删除“this”关键字修复了这一问题。所以我有:

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;
} 

在这种观点中:

Response.Write(Model.NavLinks(Html))

这是有效的,尽管我不完全理解为什么以前没有。

“使用System.Web.Mvc.Html”是关键,尽管我认为我在某个时候已经尝试过了;可能是在意识到需要传入项“Html”(隐式传递给视图的HtmlHelper对象?)之前。VS通常会提示缺少Using语句,但这次没有。

谢谢





相关问题
ASP.Net MVC - Handle Multiple Checkboxes

Ok, I have a role based permission system in place and would like admin s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, however I m struggling with ...

Entity Framework error when submitting empty fields

VS 2010 Beta 2, .NET 4. In my ASP.NET MVC 2 application, when I submit a form to an action method that accepts an object created by the entity framework, I get the following error: Exception Details:...

ASP.Net MVC Architecture - Location of ViewModels

We have a decent sized MVC project running well at the moment, i ve started to look at some re-factoring and I have a question. At the moment the Data Layer and Service Layer is stored in a seperate ...

ASP.Net MVC Go To Declaration On view

Is there anyway when in an MVC view to right click a function call and get to the declaration? Like you can do in the code behind/controllers

ASP.NET MVC 2 Beta - Default Model Binder

I m experiencing some different behavior after switching from ASP.NET MVC 1.0 to ASP.NET MVC 2 Beta. I checked the breaking changes but it s not clear where the issue lies. The problem has to do with ...

Asp.net MVC Dynamic Menu System

In our current Asp.net MVC application we have 2 menu systems one accross the top and one on the left hand side. Now we have a partial view that renders the menu, however is the only way for this too ...

热门标签