Skip to main content

Message Helper for MVC Application


Message Helper for MVC Application



public static class MessageExtensions
{

public enum MessageType
{
Success = 0,
Info = 1,
Warning = 2,
Error = 3
}


public static void ShowMessage(this Controller controller, MessageType messageType, string message, bool showAfterRedirect = false)
{
string messageTypeKey = messageType.ToString();
if (showAfterRedirect)
{
controller.TempData[messageTypeKey] = message;
}
else
{
controller.ViewData[messageTypeKey] = message;
}
}


public static HtmlString RenderMessages(this HtmlHelper htmlHelper, bool clearMessage = true)
{
string messages = String.Empty;
foreach (string messageType in Enum.GetNames(typeof(MessageType)))
{

object message = htmlHelper.ViewContext.ViewData.ContainsKey(messageType)
? htmlHelper.ViewContext.ViewData[messageType]
: htmlHelper.ViewContext.TempData.ContainsKey(messageType)
? htmlHelper.ViewContext.TempData[messageType]
: null;
if (message != null)
{
string _class = string.Empty;
var spanTag = new TagBuilder("span");
if (messageType == MessageType.Success.ToString())
{
//_class = "success";
_class = "alert-success";
//spanTag.SetInnerText("Success! ");
}
else if (messageType == MessageType.Error.ToString())
{
//_class = "error";
_class = "alert-danger";
// spanTag.SetInnerText("Error! ");
}
else if (messageType == MessageType.Info.ToString())
{
_class = "information";
// spanTag.SetInnerText("Information! ");
}
else if (messageType == MessageType.Warning.ToString())
{
//_class = "warning";
_class = "alert-warning";
//spanTag.SetInnerText("Warning! ");
}

var messageBoxBuilder = new TagBuilder("div");
messageBoxBuilder.Attributes["onclick"] = "$(this).fadeOut(3000)";
messageBoxBuilder.AddCssClass(string.Format("msg {0}", _class));
string close = "<a class='notification-close' href='javascript:'>×</a>";
messageBoxBuilder.InnerHtml = spanTag + message.ToString() + close;
messages += messageBoxBuilder.ToString();
ClearMessages(htmlHelper, messageType);
}
}
return MvcHtmlString.Create(messages);
}


private static void ClearMessages(HtmlHelper htmlHelper, string messageType)
{
htmlHelper.ViewContext.ViewData[messageType] = htmlHelper.ViewContext.TempData[messageType] = null;
}
}



How to use this Message Helper

1. Add this in you view

@Html.RenderMessages(true)

2.Set message from controller

ShowMessage(MessageExtensions.MessageType.Error, "Mail has not been sent.", true);



Comments

Popular posts from this blog

CRUD in EF

 public int Create(Users entity)         {             using (var context = new K305DataEntities())             {                 var newEntity = Mapper.Map<User>(entity);                             context.Users.Add(newEntity);                 context.SaveChanges();                 return newEntity.Id;             }         }         public int Update(Users entity)         {             using (var context = new K305DataEntities())             {                 var dbEntity = context.Users.FirstOrDefault(dc => dc.Id == entity.Id);                 if (dbEntity == null) throw new ApplicationException("Entity not found.");                 dbEntity.Name = entity.Name;                 context.SaveChanges();                 return dbEntity.Id;             }                 }         public List<Users> GetAll()         {             using (var context = new K305DataEntities())             {                 return context.Users.Select(

Pass Values Between ASP.NET Web Pages without Session or any State Management

If the source page and target page are both ASP.NET Web pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the transfer method, the target page can access public properties in the source page. Page One <asp:TextBox ID="textCity" runat="server" Text="Brjesh"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  public String CurrentCity         {             get             {                 return textCity.Text;             }         }         public List<String> Current         {             get             {                 return _Current;             }         }         private List<String> _Current = null;         protected void Page_Load(object sender, EventArgs e)         {             _Current = new List<String>();             _Current.Add("1