Gui*_*nge 14 c# asp.net notifications asp.net-mvc-4
我想在添加一些用户时在我的视图中显示一些消息.
当我们的模型出现问题时,有一个方法(ModelState.AddModelError)来处理不成功的消息.但是,当事情顺利的时候,我们怎样才能向用户说出他的行动是否成功?
我发现这个线程提供了解决方案,但大约三年过去了,我需要知道:没有其他方式,也许更成熟?不是这不是,但我们仍然以同样的方式处理成功的信息?
Car*_*all 11
从Brad Christie的 答案扩展而来,我创建了一个NuGet包,BootstrapNotifications,它将通过内置的Bootstrap3支持为您完成.此软件包还支持多种通知类型(错误,警告,成功和信息),并具有预先设置的警报,并且可以轻松扩展.
该扩展支持每个相同类型和不同类型的请求的多个通知.
NotificationExtensions.cs:
public static class NotificationExtensions
{
private static IDictionary<String, String> NotificationKey = new Dictionary<String, String>
{
{ "Error", "App.Notifications.Error" },
{ "Warning", "App.Notifications.Warning" },
{ "Success", "App.Notifications.Success" },
{ "Info", "App.Notifications.Info" }
};
public static void AddNotification(this ControllerBase controller, String message, String notificationType)
{
string NotificationKey = getNotificationKeyByType(notificationType);
ICollection<String> messages = controller.TempData[NotificationKey] as ICollection<String>;
if (messages == null)
{
controller.TempData[NotificationKey] = (messages = new HashSet<String>());
}
messages.Add(message);
}
public static IEnumerable<String> GetNotifications(this HtmlHelper htmlHelper, String notificationType)
{
string NotificationKey = getNotificationKeyByType(notificationType);
return htmlHelper.ViewContext.Controller.TempData[NotificationKey] as ICollection<String> ?? null;
}
private static string getNotificationKeyByType(string notificationType)
{
try
{
return NotificationKey[notificationType];
}
catch (IndexOutOfRangeException e)
{
ArgumentException exception = new ArgumentException("Key is invalid", "notificationType", e);
throw exception;
}
}
}
public static class NotificationType
{
public const string ERROR = "Error";
public const string WARNING = "Warning";
public const string SUCCESS = "Success";
public const string INFO = "Info";
}
Run Code Online (Sandbox Code Playgroud)
_Notifications.cshtml:
@using YourApp.Extensions
@{
var errorList = Html.GetNotifications(NotificationType.ERROR);
var warningList = Html.GetNotifications(NotificationType.WARNING);
var successList = Html.GetNotifications(NotificationType.SUCCESS);
var infoList = Html.GetNotifications(NotificationType.INFO);
}
<!-- display errors -->
@if (errorList != null)
{
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@if(errorList.Count() > 1){
<strong><span class="glyphicon glyphicon-remove"></span> There are @errorList.Count() errors: </strong>
<ul>
@foreach (String message in errorList)
{
<li>@Html.Raw(message)</li>
}
</ul>
}
else{
<strong><span class="glyphicon glyphicon-remove"></span> Error: </strong>
@Html.Raw(errorList.First())
}
</div>
}
<!-- display warnings -->
@if (warningList != null)
{
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@if(warningList.Count() > 1){
<strong><span class="glyphicon glyphicon-warning-sign"></span> There are @warningList.Count() warnings: </strong>
<ul>
@foreach (String message in warningList)
{
<li>@Html.Raw(message)</li>
}
</ul>
}
else{
<strong><span class="glyphicon glyphicon-warning-sign"></span> Warning: </strong>
@Html.Raw(warningList.First())
}
</div>
}
<!-- display success -->
@if (successList != null)
{
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@if(successList.Count() > 1){
<strong><span class="glyphicon glyphicon-ok"></span> There are @successList.Count() successful notifications: </strong>
<ul>
@foreach (String message in successList)
{
<li>@Html.Raw(message)</li>
}
</ul>
}
else{
<strong><span class="glyphicon glyphicon-ok"></span> Success! </strong>
@Html.Raw(successList.First())
}
</div>
}
<!-- display success -->
@if (infoList != null)
{
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@if(infoList.Count() > 1){
<strong><span class="glyphicon glyphicon-info-sign"></span> There are @infoList.Count() notifications: </strong>
<ul>
@foreach (String message in infoList)
{
<li>@Html.Raw(message)</li>
}
</ul>
}
else{
<strong><span class="glyphicon glyphicon-info-sign"></span> </strong>
@Html.Raw(infoList.First())
}
</div>
}
Run Code Online (Sandbox Code Playgroud)
要查看所有这些代码及其使用方法,您可以从github下载完整的工作演示.
Bra*_*tie 10
TempData为了通知用户,将一次性交给UI并不是一种坏方法.关于他们的重要部分是他们在行动呼叫之间坚持,但一旦被阅读就被删除.因此,在仅仅传递"工作"消息的情况下,它的效果很好.
您可以通过多种方式绑定它们,但我会给您一个通用示例来帮助您:
public static class NotificationExtensions
{
private const String NotificationsKey = "MyApp.Notifications";
public static void AddNotification(this ControllerBase controller, String message)
{
ICollection<String> messages = controller.TempData[NotificationsKey] as ICollection<String>;
if (messages == null)
{
controller.TempData[NotificationsKey] = (messages = new HashSet<String>());
}
messages.Add(message);
}
public static IEnumerable<String> GetNotifications(this HtmlHelper htmlHelper)
{
return htmlHelper.ViewContext.Controller.TempData[NotificationsKey] as ICollection<String> ?? new HashSet<String>();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在您的操作中,您可以调用this.AddNotification("User successfully added!");并在视图中使用以下内容显示它们:
@foreach (String notification in Html.GetNotifications())
{
<div class="notification">
<p>@notification/p>
<i class="icon-close"></i>
</div>
}
Run Code Online (Sandbox Code Playgroud)
(......或类似的东西),可以有效地放在主视图中,并用作执行任何操作的一般通知方法.(几乎就像StackOverflow在某些事件期间页面顶部有金条一样).
有几种方法可以给这只猫上皮.您可以使用ViewBag:
ViewBag.SuccessMessage = "<p>Success!</p>";
Run Code Online (Sandbox Code Playgroud)
然后在您的视图中,您可以将其呈现到页面:
@ViewBag.SuccessMessage
Run Code Online (Sandbox Code Playgroud)
我不是ViewBag的粉丝,所以我通常会创建一个ViewModel对象,它包含我需要的特定视图所需的所有数据.成功的消息就是那种数据:
public MyViewModel{
public bool IsSuccess {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的控制器中,您将此ViewModel传递给您的stongly类型视图
[HttpPost]
public ActionResult Update(MyViewModel vm){
//Glorious code!
return View(vm)
}
Run Code Online (Sandbox Code Playgroud)
最后,只需在您的视图中检查它并在成功时打印一条消息:
@if(vm.IsSuccess){
<p>Here is an amazing success message!</p>
}
Run Code Online (Sandbox Code Playgroud)
另外,您可以使用TempData,它可以像ViewBag一样工作,但只会持续到下一个请求结束,然后被丢弃:
TempData["SuccessMessage"] = "Success!";
Run Code Online (Sandbox Code Playgroud)
一个很好的解决方案是集合TempData。它的值在请求结束时被清除,这使得它非常适合一次性消息,例如通知用户某件事已成功。
控制器
TempData["Message"] = "Operation successful!";
Run Code Online (Sandbox Code Playgroud)
看法
@TempData["Message"]
Run Code Online (Sandbox Code Playgroud)
是的,这仍然是目前最好的方法。
| 归档时间: |
|
| 查看次数: |
24705 次 |
| 最近记录: |