2

I'm trying to create a helper function within ASP.NET MVC and pass a model along with one of its attributes as parameters. I'm trying to mimic some of the other helper functions that already exist within this project I'm working on but I keep getting Context errors.

EDIT:

Here is the helper function I am trying to create:

public static MvcHtmlString PreviousPage(this HtmlHelper helper, IPagedList<QuestionAnswerModel> Answers, int pageCount)
    {

        var tag = new TagBuilder("link");
        tag.MergeAttribute("rel", "prev");

        return MvcHtmlString.Create(tag.ToString());
    }

And I am trying to access this helper within my View using Razor:

@{ @Html.PreviousPage(Model.Answers, Model.Answers.PageCount) }

EDIT:

Here is the error I am receiving and it is happening from my View code above:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'HtmlHelper<QuestionAnswersModel>' does not contain a definition for 'PreviousPage' and no extension method 'PreviousPage' accepting a first argument of type 'HtmlHelper<QuestionAnswersModel>' could be found (are you missing a using directive or an assembly reference?)

I know this syntax isn't correct but I do know that my Model is correct and these attributes are defined properly.

Any help is greatly appreciated!

10
  • 1
    "I keep getting Context errors." - what are Context errors, what is their text exactly and what did your research for those error messages show? Commented Sep 25, 2017 at 18:56
  • Your extension method actually only takes one argument now. Remember that the "this" argument is whatever object you call the function on. Commented Sep 25, 2017 at 18:58
  • @juunas I'm going to make an edit above so you'll see the original error I was receiving. Commented Sep 25, 2017 at 19:00
  • As @CodeCaster already pointed out if you have compile time errors in your code you need to include the details of those errors and include the Line of Code it occurs on, the full error message, and the error code. Commented Sep 25, 2017 at 19:03
  • 1
    So the problem is your extension method is probably found in a different namespace. You can either: Add the namespace to the top of the razor file, change your namespace of your extension method class, or edit the web.config found in the Views sub directory of your project to include that namespace as the default namespace. Commented Sep 25, 2017 at 19:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.