tags:

views:

65

answers:

3

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...

Section Controller

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();
        }

    }
}

Home Controller

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();
        }
    }
}

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?

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

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?

A: 

HomeController.Index isn't calling SectionController.Section.

brian
A: 

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";
}
Richard Ev
This practically works but a constructor is meant to be used to build an object. I do not think placing some data into ViewData has anything to do with building SectionController.
Serhat Özgel
I see where I went wrong i'm was trying to return an ActionResult but all I wanted was to make the information avaiable. I've edited my post with my current solution.
Jemes
I am not saying your solution is wrong or it does not work, it surely works. I am just saying constructor is semantically a wrong place to fill in viewdata.
Serhat Özgel
A: 

Because none of the requests are mapped to action "Section" in SectionController. If you mapped a request like domain/Section/Section, you would see your message in your view (Assuming that you are using default routing and have a view named "Section").

What you need to do is, placing your message into the viewdata on a method that runs every time an action is run. You can do it in OnActionExecuting like:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    ViewData["Message"] = "THIS IS A TEST";
    base.OnActionExecuting(filterContext);
}

in the SectionController.

Serhat Özgel