Flash equivalent in ASP.NET MVC 3

后端 未结 8 1513
情深已故
情深已故 2020-12-07 15:38

There is a feature called \'flash\' in ruby on rails where you can put a message in \'flash\', redirect, and the message is available in the next action.

Example of

8条回答
  •  我在风中等你
    2020-12-07 16:26

    Endy,

    I 'borrowed' this from the tekpub series:

    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("");
                }
                return sb.ToString();
            }
    
        }
    }
    

    typical usage (inside controller):

    public ActionResult Delete(int id, FormCollection collection)
    {
        var item = _session.Single(x=>x.ID == id);
        try
        {
            _session.Delete(item);
            _session.CommitChanges();
            this.FlashInfo("UserAction deleted ...");
            return RedirectToAction("Index");
        }
        catch
        {
            this.FlashError("There was an error deleting this record");
            return View("Edit",item);
        }
    }
    

    the css is pretty straightfwd too:

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

    and in your site.master

    <%=Html.Flash() %>
    
        
    .... etc
    
    

    enjoy...

提交回复
热议问题