简体   繁体   中英

Creating an extension method in C#

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 categoryRepo = 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).

您需要为HtmlHelper引用System.Web.Mvc ,但扩展名在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

我不确定添加命名空间请参阅resukt System.Web.Mvc.Html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM