8

I would like to access the TempData in my helper for a flash message (like in ruby)

I get a runtime error of

The name 'TempData' does not exist in the current context

my Flash.cshtml is as follows

@helper Show() 
{
    var message = "test message";
    var className = "info";

    if (TempData["info"] != null)
    {
        message = TempData["info"].ToString();
        className = "info";
    }
    else if (TempData["warning"] != null)
    {
        message = TempData["warning"].ToString();
        className = "warning";
    }
    else if (TempData["error"] != null)
    {
        message = TempData["error"].ToString();
        className = "error";
    } 

    <script>
        $(document).ready(function () {
            $('#flash').html('@HttpUtility.HtmlEncode(message)');
            $('#flash').toggleClass('@className');
            $('#flash').slideDown('slow');
            $('#flash').click(function () { $('#flash').toggle('highlight') });
        });
    </script>
}

in the layout i have

<section id="main">
    @Flash.Show() 
    <div id="flash" style="display: none"></div>
    @RenderBody()
</section>
eiu165
  • 6,101
  • 10
  • 41
  • 59

4 Answers4

13

TempData belongs to ControllerBase class which is base class for controllers, it's not accessible to shared views which no controller is behind them,

One possible workaround is to pass the controller to your helper method or access it through HtmlHelper.

@helper SomeHelper(HtmlHelper helper)
{
  helper.ViewContext.Controller.TempData
}
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • i was able to use this answer to do the same thing although not in a helper http://stackoverflow.com/a/5582533/31470 thanks – eiu165 Jan 08 '12 at 14:58
  • Nonononono please you do NOT want to pass a -controller- to a -view-. How about you set up a filter that automatically pushes the TempData into the view bag? Or use the OnActionExecut{ing,ed} methods in a controller base class to populate the viewbag with the data. Directly poking controller methods / fields / foo from within a view pretty much breaks encapsulation. The difference between the two examples is that the linked solution does not use the controller in the view. – Cornelius Feb 02 '12 at 15:29
  • You can now use helper.ViewContext.TempData. – Kevin Swarts Feb 28 '15 at 00:55
4

Just pass TempData to your helper.

The call to the helper in your layout will look like this.

@Flash.Show(TempData)

Your Flash.cshtml helper will look like this.

@helper Show(System.Web.Mvc.TempDataDictionary tempData)
{
    // The contents are identical to the OP's code,
    // except change all instances of TempData to tempData.
}
0

Some also use TempData in order to help data survive a redirect. If that is the case you can fix your problem by first assigning data to TempData:

TempData["myStuff"] = myData;

Then inside your new redirected action:

ViewBag["myBaggedStuff"] = TempData["myStuff"];

Then use ViewBag in your shared View.

RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
0

It looks like you're using TempData where you really want to be using the ViewBag or ViewData["key"].

Controller

ViewBag.info=someString;
return View(model);

View

if (ViewBag.info != null)
{
    message = ViewBag.info;
    className = "info";
}

Check out this article: http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx

Rondel
  • 4,811
  • 11
  • 41
  • 67