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!

Jeff P.
  • 2,754
  • 5
  • 19
  • 41
  • 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? – CodeCaster Sep 25 '17 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. – juunas Sep 25 '17 at 18:58
  • @juunas I'm going to make an edit above so you'll see the original error I was receiving. – Jeff P. Sep 25 '17 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. – Igor Sep 25 '17 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. – Igor Sep 25 '17 at 19:06
  • Marked as duplicate for now, there are multiple answers in that duplicate. If this does not solve your problem let me know and we can reopen the question. – Igor Sep 25 '17 at 19:08
  • @Igor I actually am including the same namespace of this helper function that other helpers within my project are accessing. – Jeff P. Sep 25 '17 at 19:10
  • @JeffP. - can you rephrase that or add some details about what you mean? Your last comment is not making much sense to me. – Igor Sep 25 '17 at 19:12
  • @Igor I actually just fixed my problem by `cleaning` my solution within Visual Studio. I'm going to edit this question soon. – Jeff P. Sep 25 '17 at 19:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155287/discussion-between-jeff-p-and-igor). – Jeff P. Sep 26 '17 at 02:14

0 Answers0