gpt4 book ai didi

c# - 从 Controller 到 View 的成功消息

转载 作者:可可西里 更新时间:2023-11-01 07:51:02 26 4
gpt4 key购买 nike

目标

我想在添加某些用户时在我的 View 中显示一些消息。

问题

当我们的模型出现问题时,有一个方法 (ModelState.AddModelError) 来处理不成功的消息。但是,当一切顺利时,我们如何处理向用户发送的消息,表明他的操作已成功?

我找到了 this thread提供了一个解决方案,但大约三年过去了,我需要知道:没有另一种方法,也许更成熟?并不是说这不是,但我们仍然以同样的方式处理成功消息?

最佳答案

Brad Christie's 展开answer ,我创建了一个 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";

}

_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">&times;</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">&times;</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">&times;</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">&times;</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>
}

要查看所有这些代码及其使用方法,您可以从 github 下载完整的工作演示。 .

关于c# - 从 Controller 到 View 的成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337914/

26 4 0
文章推荐: php - 语法错误或访问冲突 : 1067 Invalid default value for 'created_at'
文章推荐: android - 如何用一种语言显示字符串资源并将其存储在其他语言中?
文章推荐: c# - 将 List 转换为 List,类型在运行时已知