Saber Amani
Saber Amani

Reputation: 6489

Html Extension methods are not accessible in MVC Partial Pages

Hi I'm trying to create a declarative Html helper method inside MVC partial page, everything works fine. But when I want to use built-in Html extension methods, I see there are no extension methods. Also I checked my view's webconfig file to add System.Web.Mvc.Html namespace. everything is OK, but I don't know why it's not working.

Any advice will be helpful.

Edit : Here is my code :

@using WebVoter.ViewModel
@using System.Web.Mvc.Html

@helper GetVoteList(IList<VoteQuestionViewModel> voteQuestionList)
{

    <div class="head-panel">
        @*For example ActionLink is not accessible here*@
        @Html.ActionLink(....);

    </div>
}

Upvotes: 2

Views: 1326

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039268

Inside Razor helpers you do not have reference to HtmlHelper. You need to pass it as parameter from the calling code:

@helper GetVoteList(HtmlHelper html, IList<VoteQuestionViewModel> voteQuestionList)
{
    <div class="head-panel">
        @html.ActionLink(....)
    </div>
}

and when you want to call this helper from some view you need to pass the reference:

@GetVoteList(Html, ...)

Personally I've always preferred writing extension methods to the HtmlHelper class instead of using those inline Razor helpers. They would then be used as standard helpers:

@Html.GetVoteList(...)

The advantage is that you are no longer tied to Razor. Extension methods are view engine agnostic and make transition to other view engines less painful because they are C# code. Another benefit is that they can be unit tested.

Upvotes: 2

Ibrahim ULUDAG
Ibrahim ULUDAG

Reputation: 480

There is a web.config under Views folder. Can you modify this config too?

Upvotes: 1

Related Questions