Im creating a custom field in MVC3 (C#) that I would like to access with the Html property in the views. I want to put it in its own class so that I can nicely organize code (private methods etc) because our current Html Extensions class is far too noisy.
This is what I’m looking for:
ex. @Html.CustomField...
Currently, I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Matrix.Investigator.Models.Parties;
using Matrix.Investigator.Website.Extensions;
namespace Matrix.Investigator.Website.Fields
{
public static class DetailsField
{
public static MvcHtmlString PartyDetailsField(this HtmlHelper helper, IEnumerable<IDetailsConfiguration> configuation)
{
.
.
.
[Some Code for the field]
return MvcHtmlString.Create([CODE FOR TAG].toString());
}
}
}
Anyway, when I attempt to use this on the form it doesn’t come up in intellisense:
ex: <li>@Html.PartyDetailsField(Model.IdentifierConfiguration)</li>
However, when the same code is placed in the HtmlHelperExtensions file, it works just fine. I do not even see a difference in the class definition.
Is there any light that can be shone on this situation OR am I going to have to leave it in the HtmlHelperExtensions class.
Make sure you have brought the namespace in which this extension method is defined into scope in your view:
or in the
<namespaces>tag of your~/Views/web.config(not~/web.config) to bring it into scope globally in all your Razor views.Obviously due to crapiness of Intellisense of Visual Studio in Razor pages you might need to close and reopen your view in order for it to pick it. Anyway never trust Intellisense of Visual Studio in views. Trust your intuition and knowledge. Running the application is the best way to verify the result.