There are a few ways to achieve this I think.
1) Use a ViewModel which could store a List<string> Errors that you can pass back to your view. Although to do this for all views would be very repetitive and not easy to maintain.
2) Use TempData to store the error messages instead of in your ViewModel. This way, you could check in your _Layout.cshtml if there are any items in TempData and display them in any way you wish (this would happen to all of your views).
3) Use toastr.js and the TempData approach to display a nice toast instead. Begin by implementing a POCO which includes an Enum for the different response types available in toastr.js i.e Error, Info, Success, Warning. Then, create a BaseController.cs file which your controllers will implement, see below for an example of this.
Next in your Controllers, you can call CreateNotification(AlertType.Error, "This is a test message.", "Error");
Finally, you need to put logic into your _Layout.cshtml file to make use of the notifications. Make sure you add a reference to toastr.js and its CSS file, and see below for an example of how to wire it up:
Full example:
Notification.cs
```
public class Alert
{
public AlertType Type { get; set; }
public string Message { get; set; }
public string Title { get; set; }
}
public enum AlertType
{
Info,
Success,
Warning,
Error
}
```
BaseController.cs
public override void OnActionExecuting(ActionExecutingContext context)
{
GenerateNotifications();
base.OnActionExecuting(context);
}
public void CreateNotification(Notification.AlertType type, string message, string title = "")
{
Notification.Alert toast = new Notification.Alert();
toast.Type = type;
toast.Message = message;
toast.Title = title;
List<Notification.Alert> alerts = new List<Notification.Alert>();
if (this.TempData.ContainsKey("alert"))
{
alerts = JsonConvert.DeserializeObject<List<Notification.Alert>>(this.TempData["alert"].ToString());
this.TempData.Remove("alert");
}
alerts.Add(toast);
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
string alertJson = JsonConvert.SerializeObject(alerts, settings);
this.TempData.Add("alert", alertJson);
}
public void GenerateNotifications()
{
if (this.TempData.ContainsKey("alert"))
{
ViewBag.Notifications = this.TempData["alert"];
this.TempData.Remove("alert");
}
}
Layout.cshtml
@if (ViewBag.Notifications != null)
{
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
List<Notification.Alert> obj = JsonConvert.DeserializeObject<List<Notification.Alert>>(ViewBag.Notifications, settings);
foreach (Notification.Alert notification in obj)
{
switch (notification.Type)
{
case Notification.AlertType.Success:
<script type="text/javascript">toastr.success('@notification.Message', '@notification.Title');</script>
break;
case Notification.AlertType.Error:
<script type="text/javascript">toastr.error('@notification.Message', '@notification.Title');</script>
break;
case Notification.AlertType.Info:
<script type="text/javascript">toastr.info('@notification.Message', '@notification.Title');</script>
break;
case Notification.AlertType.Warning:
<script type="text/javascript">toastr.warning('@notification.Message', '@notification.Title');</script>
break;
}
}
}