开发者

Creating an extension method in C#

开发者 https://www.devze.com 2023-01-30 06:19 出处:网络
I\'m trying to create an extension method in C# for the HtmlHelper class. I\'ve read the MSDN page for it, and I\'m sure I\'m referencing the correct namespaces. I wonder what I could be doing wrong.

I'm trying to create an extension method in C# for the HtmlHelper class. I've read the MSDN page for it, and I'm sure I'm referencing the correct namespaces. I wonder what I could be doing wrong.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; //Correctly referencing the necessary namespaces, right?

namespace MvcApplication1.HelperMethods
{
    public static class NavigationalMenu
    {
        public static string MyMenu(this HtmlHelper helper)
        {
            CategoryRepository categor开发者_StackOverflow中文版yRepo = new CategoryRepository();
            var categories = categoryRepo.FindAllCategories();

            foreach (Category c in categories)
            {
                helper.RouteLink(blablabla); //Construct links and return them.
            }

            //helper.RouteLink doesn't show up! C# wipeouuuuuttttt.
            //It's as if 'helper' doesn't have the RouteLink method there.
        }
    }
}

First time that this happens to me when programming in C#. Anyone else encounter this problem?


According to MSDN:

Extensions to the HtmlHelper class are located in the System.Web.Mvc.Html namespace. These extensions add helper methods for creating forms, rendering HTML controls, rendering partial views, input validation, and more.

Trying including the System.Web.Mvc.Html namespace. LinkExtensions.RouteLink gives its namespace as that (it says it's in System.Web.Mvc.dll, just in a different namespace).


You need to reference System.Web.Mvc for the HtmlHelper, but the extension is in System.Web.Mvc.Html.


Your namespace may be wrong. Try using System.Web.Mvc.Html;

http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper(VS.90).aspx


I am not sure add the namespace see the resukt System.Web.Mvc.Html

0

精彩评论

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