什么是传递一个确认信息或错误信息返回到美景的好办法? [英] What is a good way to pass a confirmation or error message back to a view?

查看:98
本文介绍了什么是传递一个确认信息或错误信息返回到美景的好办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击提交表单按钮我要回一个成功/失败消息给用户,我想知道的消息发回给用户的最佳方式是什么。

When the user clicks the submit button on a form I want to return a success / failure message to the user and i'm wondering what the best way to send the message back to the user is.

如果用户输入的密码不正确我愿意认为与一条消息,告诉他们,登录失败重装例如登录表单上。或在更新表单他们会显示了一个消息,说升级成功的原始视图。

For example on a login form if the user enters an incorrect password i'd want the view to reload with a message telling them that login failed. Or on an update form the original view they had would show up with a message saying update was successful.

我的想法是有寻找一个ViewBag财产像的ErrorMessage或SuccessMessage,并有条件地展现自我,当这些属性都有一个价值几部分观点或HTML佣工。这些组件可以被添加到在需要它们的_layout或手动每个表单

My idea is to have a few partial views or HTML helpers that look for a ViewBag property like "ErrorMessage" or "SuccessMessage" and conditionally show themselves when these properties have a value. These components could be added to the _Layout or manually each form where they are required.

修改

我后来发现,在的ValidationSummary 方法利用是否排除属性的错误,这意味着你可以用它来表明,当登录/注册失败的参数。但是,似乎在哪里,如果没有错误的验证摘要仍在甚至产生错误。

I have since found out that the ValidationSummary method takes a parameter of whether to exclude property errors which means you can use it to show when login / registration has failed. But there seems to be a bug where the validation summary is still generated even if there are no errors.

推荐答案

这是一个很好的问题,一个我一直想在一个新的MVC应用程序来解决最近。对我来说,ModelState.AddModelError是确定错误,但通信之类的东西成功或警告信息时,无法正常工作。

This is a good question and one that I've been trying to solve in a new MVC application recently. For me, ModelState.AddModelError is ok for errors but doesn't work when communicating things like success or warning messages.

下面是一个办法,我决定最近采取和我欢迎任何反馈:

Here's an approach that I decided to take recently and I welcome any feedback on it:


  1. 我创建了一个叫做类的通知的,并呼吁枚举的 NotificationType 的。的 NotificationType 的有4个值:成功错误警告的,和放大器; 信息的。的通知的有属性'信息'和'类型'。

  1. I created a class called Notification and an enum called NotificationType. NotificationType has 4 values: Success, Error, Warning, & Info. Notification has the properties 'Message' and 'Type'.

我有一个叫静态类的 NotificationManager 的保存通知对象的实例为Htt的prequest的生命(使用该用户的会话对象)。这个想法是,任何控制器可以添加任何数量的通知的取决于它所需要告诉用户动作的执行过程中的对象。

I have a static class called NotificationManager that holds instances of Notification objects for the life of an HttpRequest (using the user's Session object). The idea is that any controller can add any number of Notification objects during the execution of an Action depending on what it needs to "tell" the user.

我有一个自定义的HtmlHelper我在一个母版页(即Html.NotificationSummaryFor()),这需要通知的IEnumerable的(即NotificationManager.Notifications),并呈现一个UL与儿童LI的每一个电话通知一次。然后我用CSS类样式的通知基于NotificationType(红色为错误,黄色代表警告,绿色表示成功,等等)LI的。该的HtmlHelper还负责他们渲染视图后清零从经理通知。

I have a custom HtmlHelper which I call once in a master page (i.e. Html.NotificationSummaryFor()) that takes a IEnumerable of Notifications (i.e. NotificationManager.Notifications) and renders a UL with child LI's for every Notification. I then use CSS classes to style those notification LI's based on the 'NotificationType' (Red for Error, Yellow for Warning, Green for Success, etc). The HtmlHelper is also responsible for clearing the notifications from the manager after rendering them to the View.

这实现是pretty简单,有一定的局限性,但在一般情况下,我觉得有这样一个框架,确保了球队在同一个应用程序的开发人员都能够以一致的方式是重要的提供通知可用性和可维护性。

This implementation is pretty simple and has limitations but in general, I find that having a framework like this ensures that a team of developers working on the same application are able to provide notifications in a consistent fashion which is important to usability and maintainability.

这篇关于什么是传递一个确认信息或错误信息返回到美景的好办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆