Created
August 22, 2016 12:43
-
-
Save saad749/89293084defd99378df73b06d9e35fac to your computer and use it in GitHub Desktop.
Easy Generic way to Display Alerts in ASP.Net Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@if (ViewBag.Messages != null) | |
{ | |
@foreach (var msg in ViewBag.Messages) | |
{ | |
<div class="alert [email protected] alert-dismissible fade in" role="alert"> | |
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
<p><strong>@msg.AlertTitle</strong> @msg.AlertMessage</p> | |
</div> | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
Html.RenderPartial("_Alerts"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AlertViewModel | |
{ | |
public string AlertType { get; set; } | |
public string AlertTitle { get; set; } | |
public string AlertMessage { get; set; } | |
public AlertViewModel(string type, string title, string message) | |
{ | |
AlertType = type; | |
AlertTitle = title; | |
AlertMessage = message; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IActionResult Index() | |
{ | |
ViewBag.Messages = new[] { | |
new AlertViewModel("success", "Success!", "The object was added successfully!"), | |
new AlertViewModel("warning", "Warning!", "The object was added with a warning!"), | |
new AlertViewModel("danger", "Danger!", "The object was not added!") | |
}; | |
return View(); | |
} |
look very nice and clear code but how you show it ?
conditional display in _alerts.cshtml
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
look very nice and clear code but how you show it ?