Asp.net mvc 在助手错误中使用TempData:名称';TempData';在当前上下文中不存在

Asp.net mvc 在助手错误中使用TempData:名称';TempData';在当前上下文中不存在,asp.net-mvc,asp.net-mvc-3,html-helper,view-helpers,Asp.net Mvc,Asp.net Mvc 3,Html Helper,View Helpers,我想访问我的助手中的TempData以获取flash消息(如在ruby中) 我得到一个运行时错误 The name 'TempData' does not exist in the current context 我的Flash.cshtml如下 @helper Show() { var message = "test message"; var className = "info"; if (TempData["info"] != null) {

我想访问我的助手中的TempData以获取flash消息(如在ruby中)

我得到一个运行时错误

The name 'TempData' does not exist in the current context
我的Flash.cshtml如下

@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>
}
@helper Show()
{
var message=“测试消息”;
var className=“info”;
if(TempData[“info”!=null)
{
message=TempData[“info”].ToString();
className=“info”;
}
else if(TempData[“warning”]!=null)
{
message=TempData[“警告”].ToString();
className=“警告”;
}
else if(TempData[“error”]!=null)
{
message=TempData[“error”].ToString();
className=“错误”;
} 
$(文档).ready(函数(){
$('#flash').html('@HttpUtility.HtmlEncode(message)');
$(“#flash”).toggleClass(“@className”);
$('flash')。向下滑动('slideDown');
$('flash')。单击(函数(){$('flash')。切换('highlight')});
});
}
在我的布局中

<section id="main">
    @Flash.Show() 
    <div id="flash" style="display: none"></div>
    @RenderBody()
</section>

@Flash.Show()
@RenderBody()

看起来您使用的是
TempData
,而您真正想使用的是
ViewBag
ViewData[“key”]

控制器

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

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

查看本文:

TempData属于
ControllerBase
类,它是控制器的基类,没有控制器支持的共享视图无法访问它

一种可能的解决方法是将控制器传递给助手方法或通过HtmlHelper访问它

@helper SomeHelper(HtmlHelper helper)
{
  helper.ViewContext.Controller.TempData
}

有些人还使用TempData来帮助数据在重定向过程中幸存下来。如果是这种情况,您可以通过首先将数据分配给TempData来解决问题:

TempData["myStuff"] = myData;
然后在新的重定向操作中:

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

然后在共享视图中使用ViewBag

只需将TempData传递给您的助手即可

在布局中对帮助器的调用如下所示

@Flash.Show(TempData)
@helper Show(System.Web.Mvc.TempDataDictionary tempData)
{
    // The contents are identical to the OP's code,
    // except change all instances of TempData to tempData.
}
您的Flash.cshtml助手将如下所示

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

我可以用这个答案来做同样的事情,虽然不是在一个助手中感谢Nonnonono请不要将-controller-传递给-view-。设置一个过滤器,自动将TempData推送到视图包中,怎么样?或者在控制器基类中使用OnActionExecut{ing,ed}方法来用数据填充viewbag。从视图中直接插入控制器方法/字段/foo几乎破坏了封装。这两个示例之间的区别在于链接的解决方案不使用视图中的控制器。您现在可以使用helper.ViewContext.TempData。