gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 3 中的 Flash 等效项

转载 作者:行者123 更新时间:2023-12-03 02:11:18 25 4
gpt4 key购买 nike

Ruby on Rails 中有一个名为“flash”的功能,您可以将消息放入“flash”中,进行重定向,并且该消息将在下一个操作中可用。

闪光灯使用示例:

有一个 Controller 操作Account.ChangePassword。如果密码更改成功,ChangePassword 将在闪存中填充“密码更改成功”消息,然后重定向到 Account.Profile。在 Account.Profile 中,该消息可用,因此可以在个人资料页面中显示。

ASP.NET MVC 3 中有等效的东西吗?

我知道我可以使用 tempdata 自己构建此功能,但是 MVC 3 是否有内置功能?

最佳答案

恩迪,

我从 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();
}

}
}

典型用法( Controller 内部):

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);
}
}

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;
}

在您的 site.master 中

<%=Html.Flash() %>
<body>
<div id="flash" style="display: none">
</div>
.... etc
</body>

享受...

关于asp.net-mvc - ASP.NET MVC 3 中的 Flash 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5581214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com