use TempData in a Helper error: The name 'TempData' does not exist in the current context
Asked Answered
V

4

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>
Visigoth answered 6/1, 2012 at 18:35 Comment(0)
R
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
}
Rataplan answered 6/1, 2012 at 18:43 Comment(3)
i was able to use this answer to do the same thing although not in a helper https://mcmap.net/q/439873/-flash-equivalent-in-asp-net-mvc-3 thanksVisigoth
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.Tachylyte
You can now use helper.ViewContext.TempData.Niece
G
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.
}
Grand answered 26/6, 2014 at 16:38 Comment(0)
B
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

Blip answered 6/1, 2012 at 18:41 Comment(0)
S
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.

Shinar answered 20/12, 2013 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.