我想在我的帮助程序中访问TempData以便于生成一个类似Ruby中的flash消息。
我遇到了一个运行时错误。
我的Flash.cshtml代码如下:
在布局中,我有:
我遇到了一个运行时错误。
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>
}
在布局中,我有:
<section id="main">
@Flash.Show()
<div id="flash" style="display: none"></div>
@RenderBody()
</section>