Creating an extension method in C #

I am trying to create an extension method in C # for the HtmlHelper class. I read the MSDN page for it, and I am sure that I am referencing the correct namespaces. I wonder what I can do 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 as if 'helper' doesn't have the RouteLink method there.
        }
    }
}

The first time this happens to me when programming in C #. Anyone else run into this problem?

+3
source share
4 answers

According to MSDN :

Extensions of the HtmlHelper class located in System.Web.Mvc.Html Namespace. These extensions add helper methods for creating forms, managing HTML, partial views, input validation, etc.

System.Web.Mvc.Html. LinkExtensions.RouteLink ( System.Web.Mvc.dll, ).

+4

System.Web.Mvc HtmlHelper, System.Web.Mvc.Html.

+3

Your namespace may be wrong. Give it a try using System.Web.Mvc.Html;

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

0
source

I am not sure what to add namespace to Resukt System.Web.Mvc.Html

0
source

Source: https://habr.com/ru/post/1780200/


All Articles