在轨道上的ruby中有一个名为"flash"的功能,您可以将消息放入"flash",重定向,并在下一个操作中显示该消息.
使用flash的例子:
有一个控制器操作Account.ChangePassword.如果密码更改成功,ChangePassword将使用"密码更改成功"消息填充闪存,然后重定向到Account.Profile.在Account.Profile中,该消息可用,因此可以在配置文件页面中显示.
ASP.NET MVC 3中是否有相同的东西?
我知道我可以使用tempdata自己构建这个功能,但是MVC 3内置了什么?
jim*_*lan 24
恩迪,
我从tekpub系列中借用了这个:
namespace System.Web.Mvc {
public static class FlashHelpers {
public static void FlashInfo(this Controller controller,string message) {
controller.TempData["info"] = message;
}
public static void FlashWarning(this Controller controller, string message) {
controller.TempData["warning"] = message;
}
public static void FlashError(this Controller controller, string message) {
controller.TempData["error"] = message;
}
public static string Flash(this HtmlHelper helper) {
var message = "";
var className = "";
if (helper.ViewContext.TempData["info"] != null) {
message =helper.ViewContext.TempData["info"].ToString();
className = "info";
} else if (helper.ViewContext.TempData["warning"] != null) {
message = helper.ViewContext.TempData["warning"].ToString();
className = "warning";
} else if (helper.ViewContext.TempData["error"] != null) {
message = helper.ViewContext.TempData["error"].ToString();
className = "error";
}
var sb = new StringBuilder();
if (!String.IsNullOrEmpty(message)) {
sb.AppendLine("<script>");
sb.AppendLine("$(document).ready(function() {");
//sb.AppendFormat("$('#flash').html('{0}');", message);
sb.AppendFormat("$('#flash').html('{0}');", HttpUtility.HtmlEncode(message));
sb.AppendFormat("$('#flash').toggleClass('{0}');", className);
sb.AppendLine("$('#flash').slideDown('slow');");
sb.AppendLine("$('#flash').click(function(){$('#flash').toggle('highlight')});");
sb.AppendLine("});");
sb.AppendLine("</script>");
}
return sb.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
典型用法(内部控制器):
public ActionResult Delete(int id, FormCollection collection)
{
var item = _session.Single<UserActions>(x=>x.ID == id);
try
{
_session.Delete<UserActions>(item);
_session.CommitChanges();
this.FlashInfo("UserAction deleted ...");
return RedirectToAction("Index");
}
catch
{
this.FlashError("There was an error deleting this record");
return View("Edit",item);
}
}
Run Code Online (Sandbox Code Playgroud)
css也非常直观:
.info
{
background-color: #CCFFCC;
border-top: 1px solid #FFCC66;
border-bottom: 4px solid #FFCC66;
padding: 6px;
font-family: helvetica;
font-size: 1.1em;
text-align: center;
border-top-color: #006600;
border-bottom-color: #006600;
font-weight: bold;
color: #339933;
cursor:pointer;
}
.warning
{
background-color: #FFFF99;
border-top: 1px solid #FFCC66;
border-bottom: 4px solid #FFCC66;
padding: 6px;
font-family: helvetica;
font-size: 0.9em;
text-align: center;
border-top-color: #CC9900;
border-bottom-color: #CC9900;
font-weight: bold;
color: #663300;
cursor:pointer;
}
.error
{
background-color: #FFCC99;
border-top: 1px solid #FFCC66;
border-bottom: 4px solid #FFCC66;
padding: 4px;
font-family: helvetica;
font-size: 1.1em;
text-align: center;
border-top-color: #800000;
border-bottom-color: #800000;
font-weight: bold;
color: #990000;
cursor:pointer;
}
Run Code Online (Sandbox Code Playgroud)
并在您的site.master
<%=Html.Flash() %>
<body>
<div id="flash" style="display: none">
</div>
.... etc
</body>
Run Code Online (Sandbox Code Playgroud)
请享用...
Tyl*_*ong 17
我重构了Imran的答案,使代码更短:
助手/ FlashHelper.cs
namespace System.Web.Mvc
{
public enum FlashEnum
{
Success = 1,
Info = 2,
Warning = 3,
Error = 4
}
public static class FlashHelper
{
public static void Flash(this Controller controller, string message,
FlashEnum type = FlashEnum.Success)
{
controller.TempData[string.Format("flash-{0}",
type.ToString().ToLower())] = message;
}
}
}
Run Code Online (Sandbox Code Playgroud)
App_Code文件/ Flash.cshtml
@helper FlashMessage(System.Web.Mvc.TempDataDictionary tempData)
{
var flash = tempData.Where(item => item.Key.StartsWith("flash-"))
.Select(item =>
new { Message = item.Value, ClassName = item.Key }).FirstOrDefault();
if (flash != null)
{
<script type="text/javascript">
$(function () {
var $flash = $('<div id="flash" style="display:none;">');
$flash.html('@flash.Message');
$flash.toggleClass('flash');
$flash.toggleClass('@flash.ClassName');
$('body').prepend($flash);
$flash.slideDown('slow');
$flash.click(function () { $(this).slideToggle('highlight'); });
});
</script>
}
}
Run Code Online (Sandbox Code Playgroud)
从twitter bootstrap借来的CSS代码
/* Styles for flash messages
-----------------------------------------------------------*/
.flash
{
padding: 8px 35px 8px 14px;
margin-bottom: 18px;
border: 1px solid;
}
.flash-success
{
color: #468847;
background-color: #DFF0D8;
border-color: #D6E9C6;
}
.flash-info
{
color: #3A87AD;
background-color: #D9EDF7;
border-color: #BCE8F1;
}
.flash-warning
{
color: #C09853;
background-color: #FCF8E3;
border-color: #FBEED5;
}
.flash-error
{
color: #B94A48;
background-color: #F2DEDE;
border-color: #EED3D7;
}
Run Code Online (Sandbox Code Playgroud)
控制器内部使用:
this.Flash("Huston, we have an error!!", FlashEnum.Error);
Run Code Online (Sandbox Code Playgroud)
布局内的用法(或其他cshtml文件):
@Flash.FlashMessage(TempData)
Run Code Online (Sandbox Code Playgroud)
Imr*_*hid 16
我想升级Jim的答案,以使用MVC 3的新辅助函数.
辅助函数可以轻松编写主要返回Html/javascript的函数,因此您不必使用字符串生成器或字符串连接.它会产生更清晰的代码.
FlashHelpers.cs:
namespace System.Web.Mvc {
public static class FlashHelpers {
public static void FlashInfo(this Controller controller,string message) {
controller.TempData["info"] = message;
}
public static void FlashWarning(this Controller controller, string message) {
controller.TempData["warning"] = message;
}
public static void FlashError(this Controller controller, string message) {
controller.TempData["error"] = message;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你创建ASP.NET App_Code文件夹并在那里创建一个.cshtml文件(可能是Flash.cshtml)并粘贴在下面的代码中
App_Code/Flash.cshtml:
@helper FlashMessage(TempDataDictionary tempData){
var message = "";
var className = "";
if (tempData["info"] != null)
{
message = tempData["info"].ToString();
className = "flashInfo";
}
else if (tempData["warning"] != null)
{
message = tempData["warning"].ToString();
className = "flashWarning";
}
else if (tempData["error"] != null)
{
message = tempData["error"].ToString();
className = "flashError";
}
if (!String.IsNullOrEmpty(message))
{
<script type="text/javascript">
$(document).ready(function() {
$('#flash').html('@message');
$('#flash').toggleClass('@className');
$('#flash').slideDown('slow');
$('#flash').click(function(){$('#flash').toggle('highlight')});
});
</script>
}
}
Run Code Online (Sandbox Code Playgroud)
这正在执行Flash功能之前正在做的事情,但是以更清洁的方式.
除了你的称呼之外,其余的东西和吉姆的答案一样.而不是使用@ Html.Flash(),你需要像这样调用它:
@Flash.FlashMessage(TempData)
Run Code Online (Sandbox Code Playgroud)
请注意,上面一行中的Flash是App_Code文件夹中.cshtml文件的名称.
希望能帮助到你.
Gre*_*rdt 13
我知道有几种解决方案,但我一直在寻找一种纯C#解决方案.我最喜欢@ TylerLong的解决方案,尽管我想支持每种类型的多条消息.另外,这是针对ASP.NET MVC4更新的,并且由于没有必要进行配置文件更改,因此它可能也适用于其他版本的MVC框架.
MvcProject/Helpers/FlashHelper.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcProject.Helpers
{
public enum FlashLevel
{
Info = 1,
Success = 2,
Warning = 3,
Danger = 4
}
public static class FlashHelper
{
public static void Flash(this Controller controller, string message, FlashLevel level)
{
IList<string> messages = null;
string key = String.Format("flash-{0}", level.ToString().ToLower());
messages = (controller.TempData.ContainsKey(key))
? (IList<string>)controller.TempData[key]
: new List<string>();
messages.Add(message);
controller.TempData[key] = messages;
}
}
}
Run Code Online (Sandbox Code Playgroud)
MvcProject/Views/Shared/_Flash.cshtml为部分:@helper FlashMessage(System.Web.Mvc.TempDataDictionary tempData)
{
<div class="flash-messages">
@foreach (FlashLevel level in (FlashLevel[]) Enum.GetValues(typeof(FlashLevel)))
{
string type = level.ToString().ToLower();
string key = "flash-" + type;
if (tempData.ContainsKey(key))
{
IList<string> messages = (IList<string>)tempData[key];
foreach (string message in messages)
{
<p class="alert alert-@type" role="alert">@message</p>
}
}
}
</div>
}
@FlashMessage(TempData)
Run Code Online (Sandbox Code Playgroud)
_Flash部分MvcProject/Views/Shared/_Layout.cshtml@Html.Partial("_Flash")
Run Code Online (Sandbox Code Playgroud)
这将导致Flash消息包含在网页中.
最后一块拼图在你的控制器中,它现在应该有一个叫做的方法Flash(string message, FlashLevel level):
using MvcProject.Helpers;
public class FoosController : Controller
{
public ActionResult Edit(int id, FooViewModel model)
{
// ...
this.Flash("Foo was updated", FlashLevel.Success);
this.Flash("Another success message!", FlashLevel.Success);
this.Flash("But there was a slight problem...", FlashLevel.Warning);
return RedirectToAction("Edit", new { id = id });
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12502 次 |
| 最近记录: |