Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Custom Html Helper not working

I am trying to learn MVC3 from Pro ASP.NET MVC3 Framework. But i am stuck at one place where we add custom Html Helper.

I did every thing mentioned in the book, but i am not able to add the custom Html helper.

Can somebody please help.

Thanks

List.cshtml

    @model SportsStore.WebUI.Models.ProductListViewModel          


@{    
    ViewBag.Titke = "Product";
}

<!DOCTYPE html>

<html>
<head>
    <title>List</title>
</head>
<body>
    <div>
        @foreach (var p in Model.Products)
        {
            <div class="item">
                @p.Name
                @p.Description
                <h4>@p.Price.ToString("c")</h4>                
            </div>
        }
        <div class="Pager">
        @Html.PageLinks(Model.pagingInfo, x => Url.Action("List", new {page = x}))
        </div>
    </div>    
</body>
</html>

PagingHelper.Cs

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.WebUI.Models;
using System.Text;

namespace SportsStore.WebUI.HtmlHelpers
{
    public static class PagingHelper
    {
        public static MvcHtmlString PageLinks(HtmlHelper helper, PagingInfo pagingInfo, Func<int, string> pageUrl)
        {
            StringBuilder linkString = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                {
                    tag.AddCssClass("selected");
                }
                linkString.Append(tag.ToString());
            }
            return MvcHtmlString.Create(linkString.ToString());

        }
    }
}

Web.Config

 <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
    <add namespace="SportsStore.WebUI.HtmlHelpers" /> 
      </namespaces>
    </pages>
  </system.web.webPages.razor>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't create an extension method.

To make an extension method, you need to decorate the first parameter with the this keyword.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...