创建控制器基类(部分)

发布于 2024-08-09 22:11:03 字数 288 浏览 4 评论 0原文

由于我的 @html.render 操作使我的开发和产品服务器崩溃,我必须使用 partials(crap)

我尝试创建 public 部分控制器{} 类,以便我可以为所有视图设置所需的数据,但我没有运气(一切都崩溃了)。

我来自 LAMP cakePHP 背景,确实需要简单性。

我需要知道如何创建部分基本控制器(不会覆盖常规基本控制器)以及如何从类访问多个模型。

谢谢你!

Since my @html.render action crashes my dev and prod servers i have to use partials(crap).

I tried creating public partial controller{} class so i can set needed data for all my views but i am having no luck (everything breaks).

I am coming from LAMP cakePHP background and really need simplicity.

I need to know how to create a partial base controller(that doesnt override the regular base controller) and how to access multiple models from the class.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-08-16 22:11:03
public class BaseController: Controller
{
   public override OnActionExecuting(...) { ... }
   public override OnActionExecuted(... context) 
   {
       if (context.Result is ViewResult)
           ((ViewResult)context.Result).ViewData["mycommondata"] = data;
   }
   ...
}

public class MyController1: BaseController 
{
}

即只是从新的基本控制器类派生。

不过,我建议您在这里询问为什么您的 RenderPartial“崩溃” - 因为它对您来说可能是更好的方法,而且它显然不应该崩溃。

public class BaseController: Controller
{
   public override OnActionExecuting(...) { ... }
   public override OnActionExecuted(... context) 
   {
       if (context.Result is ViewResult)
           ((ViewResult)context.Result).ViewData["mycommondata"] = data;
   }
   ...
}

public class MyController1: BaseController 
{
}

I.e. just derive from your new base controller class.

However I'd suggest you to ask here why your RenderPartial "crashes" - since it can be a better way for you, and it obviously shouldn't crash.

只等公子 2024-08-16 22:11:03

创建基本控制器的更好方法,

    public class Controller : System.Web.Mvc.Controller
{
    public shipsEntities db = new shipsEntities();

    public Controller()
    {
        ViewData["ships"] = db.ships.ToList();
    }
}

使其余控制器遵循常规约定

public class MyController : Controller

better way to create base controller

    public class Controller : System.Web.Mvc.Controller
{
    public shipsEntities db = new shipsEntities();

    public Controller()
    {
        ViewData["ships"] = db.ships.ToList();
    }
}

that way the rest of controllers follow regular convention

public class MyController : Controller
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文