基础类 [英] Base controller class

查看:124
本文介绍了基础类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的控制器类,我想从基类传递一个消息给所有控制器和该消息是提供给所有的意见。

I have a base controller class and I would like to pass a Message from the Base class to all controllers and for that message to be available to all views.

我已经创建了一个基本的版本...

I've created a basic version below...

部分控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Website.Controllers
{
    public class SectionController : Controller
    {
        //
        // GET: /Section/

        public ActionResult Section()
        {
            ViewData["Message"] = "THIS IS A TEST";
            return View();
        }

    }
}

主控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Website.Controllers
{
    public class HomeController : SectionController
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

查看

<%= Html.Encode(ViewData["Message"]) %>

我知道我可以在家里控制器做到这一点,但我只是在莫测试。

I know I can do this in the home controller but I'm just testing at the mo.

我没有收到上述任何错误,但我还没有在我的视图中显示的信息?

I'm not getting any errors with the above but I'm also not displaying the message on my view?

我使用本教程 HTTP://www.asp。净/ LEARN / MVC /教程13 cs.aspx 很好的解决方案的一部分,没有什么帮助。

I'm using this tutorial http://www.asp.net/LEARN/mvc/tutorial-13-cs.aspx The Good Solution part, if that helps.

我想我已经得到了它的工作现在用下面的code我sectionController ...

Think I've got it working now used the code below on my sectionController...

namespace Website.Controllers
{
    public class SectionController : Controller
    {
        //
        // GET: /Section/

        public SectionController()
        {
            ViewData["Message"] = "THIS IS A TEST";
            //return View();
        }

    }
}

这是一个确定的解决方案?

Is this an ok solution?

推荐答案

您在设置你的的ViewData 部分你的基地控制器的操作方法,你真的想在你的基地控制器的构造函数来设置它?

You're setting your ViewData in the Section action method of your base controller, do you actually want to be setting it in the constructor of your base controller?

public SectionController()
{
    ViewData["Message"] = "THIS IS A TEST";
}

这篇关于基础类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆