1

I have the following in ~/Helpers/Helpers.cs:

namespace AdjusterSave.Helpers
{
    public class Helpers : Controller
    {
        // various methods such as the following...

        public void GetDropdowns()
        {
        }
    }
}

I am trying to use these in my ~/Controllers/AdjusterController.cs file by including it like so:

using AdjusterSave.Helpers;

However, I continue to get the following error when trying to use the methods. When I call this:

GetDropdowns();

I get this error:

The name 'GetDropdowns' does not exist in the current context.

Edit:

Trying to use method like so (in ~/Controllers/AdjusterController.cs):

public ActionResult ViewProfile()
{
    // a bunch of code like this:
    User user = db.Users.Where(x => x.username == HttpContext.User.Identity.Name).FirstOrDefault();

    AdjusterViewProfileInfo model = new AdjusterViewProfileInfo();

    // get name
    model.namePrefix = user.namePrefix;
    model.firstName = user.firstName;
    model.middleInitial = user.middleInitial;
    model.lastName = user.lastName;
    model.nameSuffix = user.nameSuffix;

    // end then, finally,

    GetDropdowns();

    // followed by...

    TempData["CurrentPage"] = "ViewProfile";

    return View("", _layout, model);
}

Edit:

GetDropdowns Example:

public void GetDropdowns(this Controller controller)
{
    // get name prefixes
    List<SelectListItem> prefixList = new List<SelectListItem>();

    prefixList.Add(new SelectListItem { Value = "Mr.", Text = "Mr." });
    prefixList.Add(new SelectListItem { Value = "Mrs.", Text = "Mrs." });
    prefixList.Add(new SelectListItem { Value = "Ms.", Text = "Ms." });

    ViewBag.PrefixList = new SelectList(prefixList, "Value", "Text");

}
7
  • Please add exact code how you're trying to use GetDropdowns() Commented Jul 15, 2013 at 14:25
  • 1
    Why is Helpers a Controller ? Why isn't the class and method static ? What are you trying to achieve ? Commented Jul 15, 2013 at 14:25
  • @empi Edit made. Thanks, Commented Jul 15, 2013 at 14:28
  • @SimonBelanger I am simply trying to be DRY and not rewrite the same methods in different controllers. My intent is to write the methods like GetDropdowns() once, and then reuse the code in multiple controllers. Commented Jul 15, 2013 at 14:29
  • 1
    @user1477388 Your class should only inherit Controller if it's going to actually be a Controller, not if it's just going to help other controllers. Commented Jul 15, 2013 at 14:47

3 Answers 3

4

You are doing it wrong. What you need to do is to create a static class like this:

public static class Helpers
{
    public static void GetDropdowns(this Controller controller)
    {
        // var username = controller.HttpContext.User.Identity.Name;

        // get name prefixes
        List<SelectListItem> prefixList = new List<SelectListItem>();

        prefixList.Add(new SelectListItem { Value = "Mr.", Text = "Mr." });
        prefixList.Add(new SelectListItem { Value = "Mrs.", Text = "Mrs." });
        prefixList.Add(new SelectListItem { Value = "Ms.", Text = "Ms." });

        controller.ViewBag.PrefixList = new SelectList(prefixList, "Value", "Text");

     }     
}

And, you can use it in your Controller like this:

this.GetDropdowns();
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but when I try to use ViewBag in my action "GetDropdowns" it says, "The name 'ViewBag' does not exist in the current context."
What ViewBag? This doesn't have anything to do with ViewBag.
If you want to use ViewBag inside the GetDropdowns method you can do it like the code above. I edited the code to demonstrate that.
Use it like this.GetDropdowns(); just like in the my answer.
Also, the GetDropdowns method has to be static.
|
2

If you want to call GetDropdowns() you can:

Instantiate Helpers and call it:

new Helpers().GetDropdowns();

Make method GetDropdowns static:

public static void GetDropdowns()
{
}

and then call it:

Helpers.GetDropdowns();

You may also inherit AdjusterController from Helpers:

public class AdjusterController : Helpers
{
}

and then call it just as you did. Everything depends on what you're interested in. I guess you should not inherit Helpers from Controller and make the method static, but it's just a guess.

1 Comment

This is what I tried initially, but I am calling ViewBag in GetDropdowns and that causes error. How can I use ViewBag in my Helper methods?
2

You can use the Extension Methods instead inheriths from Controller:

public static class Helpers
{
    public static void GetDropdowns(this Controller controller)
    {
        // do something with the "controller", for sample:
        controller.ViewBag = new List<string>();
    }
}

Everything you need to access on the controller you can do by the controller parameter.

And in your controller you can do something like this:

public ActionResult Index()
{
    // just call it, and the .net framework will pass "this" controller to your extension methodo
    GetDropdowns();
}

7 Comments

Thanks, but when I try to use ViewBag in my action "GetDropdowns" it says, "The name 'ViewBag' does not exist in the current context."
Everything you need to access on the controller you can do by the controller parameter. Take a look my edits.
Thanks, it looks like this works. However, it seems a bit cumbersome. Since this seems like a common scenario, is there no better way to do this? I am simply trying to reuse a method in multiple controllers.
That's exactly what you need to do. It's called an extension method. The whole purpose of it is to be used in multiple controllers.
Well, Extension methods are frequently used by .net framework when you want to add some functions on a class and use it as part of it (obj.Method), but remember it is still a method static. If you want to add a function that all controllers of your application should have, in this case you can create a Custom Base Controller and make your controller inherit from this one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.