5

I am working over my first application over MVC3 and still kind of a newbie in it:
I’m trying to success my ViewData[] over a master page because its contains a message that would be used over every page, but when I’m trying to access that it says:

CS0103: The name 'ViewData' does not exist in the current context

var msg = ViewData["msg"] as string;
        //var msg = ViewBag.msg as string;
        if (msg != null)
        {
            Response.Write (msg);
        } else if (msg == null)
        {
            Response.Write("");
        }

I am not sure whether I’m doing something wrong or it’s just not possible to access ViewData[] over my MasterPage. Help please!

Maven
  • 14,587
  • 42
  • 113
  • 174
  • BTW, you don't need your `if` check; `Response.Write(null)` wo't do anything. – SLaks Oct 12 '11 at 17:40
  • 1
    ok but then how would i check that whether there is some message in ViewData or not? & if there is thn print it? – Maven Oct 12 '11 at 18:20
  • You don't need to check. If you print a message that isn't there, nothing will happen, – SLaks Oct 12 '11 at 18:28
  • 1
    ok Thanks! :) but i thnk ill still be using the if as i am displaying a message in a div which will only be displayed if there is a message else it ll hidden.. so ild need to check if there is a msg or not! – Maven Oct 12 '11 at 18:46

1 Answers1

3

You need to set your master page to inherit System.Web.Mvc.ViewMasterPage.

bzlm
  • 9,626
  • 6
  • 65
  • 92
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    And stop using Response.Write. :) – bzlm Oct 12 '11 at 17:40
  • it is currently been inherited by "myweb.Views.myweb" in master directive.. should replace it? <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="myweb.master.cs" Inherits="myweb.Views.myweb" %> – Maven Oct 12 '11 at 18:27
  • 1
    You should not use WebForms code-behind files in ASP.Net MVC. Change it to `<%@ Master Language="C#" Inherits="ViewMasterPage" %>` – SLaks Oct 12 '11 at 18:29
  • ok it has just started giving me this error: "The type 'System.Web.Mvc.ViewMasterPage' is ambiguous:......" – Maven Oct 12 '11 at 19:06
  • You're probably referencing two versions of `System.Web.Mvc.dll`. Remove one of them (check both `Web.config`s). Read the entire message for more detail. – SLaks Oct 12 '11 at 19:44