[英]MVC using methods from a helpers namespace
I have the following in ~/Helpers/Helpers.cs: 我在〜/ 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: 我试图通过将其包含在我的〜/ Controllers / AdjusterController.cs文件中来使用它们,如下所示:
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. 名称“ GetDropdowns”在当前上下文中不存在。
Edit: 编辑:
Trying to use method like so (in ~/Controllers/AdjusterController.cs): 尝试使用类似这样的方法(在〜/ 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: GetDropdowns示例:
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");
}
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();
You can use the Extension Methods instead inheriths from Controller: 您可以使用扩展方法,而不是从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. 您需要通过controller
参数执行的,在controller
上需要访问的所有内容。
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();
}
If you want to call GetDropdowns() you can: 如果要调用GetDropdowns(),则可以:
Instantiate Helpers and call it: 实例化Helper并调用它:
new Helpers().GetDropdowns();
Make method GetDropdowns static: 使方法GetDropdowns静态:
public static void GetDropdowns()
{
}
and then call it: 然后调用它:
Helpers.GetDropdowns();
You may also inherit AdjusterController from Helpers: 您也可以从Helpers继承AdjusterController:
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. 一切都取决于您感兴趣的内容。我猜您不应从Controller继承Helpers并使该方法静态化,但这只是一个猜测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.