什么是推荐的方法,在MVC提供用户通知/确认? [英] What is the recommended approach to providing user notifications / confirmations in MVC?
问题描述
一个常见的情况我遇到的是提供通知/确认给用户他们已执行的行动,通知他们成功了。
A common scenario I encounter is providing notifications / confirmations to users after they have performed an action to inform them of success.
例如,假设一个用户提供了一个反馈表反馈,然后点击提交反馈即可。你可能想显示感谢您的反馈信息的在您已经例如进行一些验证他们在数据库中有一个有效的电子邮件。一些伪code:
For example, suppose a user provides feedback on a feedback form and then clicks Submit Feedback. You may want to display a 'Thanks for your Feedback' message after you have performed some validation e.g. they have a valid email in the database. Some pseudocode:
public ActionResult SubmitFeedback(string Feedback, int UserID)
{
MyDataContext db = new DataContext()
if(db.usp_HasValidEmail(UserID)) //Check user has provided a valid email
return View("Index"); //Return view and display confirmation
else
ModelState.AddModelError("InvalidEmail", "We do not hold an email record for you. Please add one below");
return View("Index);
}
我理解如何使用验证项 Html.ValidationMessage
等,这是不错,我通常检查无效条目无论是在客户端使用jQuery或早期我行动(即之前,我开始打的数据库)和是否有无效的项目退出我的行动。
I understand how to validate entries by using Html.ValidationMessage
etc. This is fine and I typically check for invalid entries either using jQuery on the client side or early in my Action (i.e. before I start hitting the database) and exit my action if there are invalid entries.
不过,关于方案是什么,所有项是有效的,你想显示确认消息?
However, what about the scenario where all entries are valid and you want to display a confirmation message?
选项1 有一个完全独立的视图
这似乎是由具有全新的视图(和视图模型)违反DRY原则,以显示几乎相同的信息,期望为用户notifcation。
This seems to violate DRY principles by having an entirely new View (and ViewModel) to display almost identical information, expect for the user notifcation.
选项2 :在视图的条件逻辑
在这种情况下,我可以有一个检查是在 SubmitFeedback
动作的推移,一些TempData的的presence查看条件语句。同样,伪code:
In this scenario I could have a conditional statement in the View that checks for the presence of some TempData that is passed in the SubmitFeedback
Action. Again, pseudocode:
<% if(TempData["UserNotification"] != null {%>
<div class="notification">Thanks for your Feedback!</div>
<% } %>
选项3 :使用jQuery来检查TempData的页面加载
在这种情况下我想有一个隐藏字段,我感觉我的TempData通过 SubmitFeedback
动作填充。然后我会使用jQuery检查隐藏字段值。更多的伪code:
In this scenario I would have a hidden field that I would populate with TempData via the SubmitFeedback
Action. I would then use jQuery to check the hidden field value. More pseudocode:
<%=Html.Hidden("HiddenField", TempData["UserNotification"])%> //in View
$(document).ready(function() {
if ($("input[name='HiddenField']").length > 0)
$('div.notification').show();
setTimeout(function() { $('div.notification').fadeOut(); }, 3000);
});
我对这个问题的初步想法是:
My initial thoughts on this are:
- 选项1:的意见完全分离,但似乎有点小题大做,效率低下(违反DRY)
- 选项2:够简单,但在查看条件逻辑(?!?难道我在MVC坛此获得牺牲)
- 选项3:感觉就像是过于复杂的事情。它避免了视图逻辑虽然。
- Option 1: Complete separation of Views but seems like overkill and inefficient (violates DRY)
- Option 2: Simple enough, but has conditional logic in the View (don't I get sacrificed at the MVC altar for this?!?)
- Option 3: Feels like it is overcomplicating things. It does avoid logic in View though.
你说呢?选项1,2,3或无?有没有更好的办法?
What say you? Option 1,2,3 or none? Is there a better way?
请我增加编码模式!
推荐答案
我喜欢选项1.你也并不需要条件,并禁用了javascript工作。只是某个地方把它贴在母版,它应该是确定:
I like option 1. Also you don't need conditions and it works with javascript disabled. Just stick it somewhere in the masterpage and it should be OK:
<div class="notification">
<%= Html.Encode(TempData["Notification"]) %>
</div>
您当然可以逐步提高/使用一些很好的插件,如 jGrowl 或的Gritter 或连看的 StackOverflow的是怎么做的的。
You could of course progressively enhance/animate this by using some nice plugin such as jGrowl or Gritter or even look at how StackOverflow does it.
另一种解决方案是编写一个帮手这可能是最巧妙的:
Another solution is to write a helper which is probably the neatest:
public static class HtmlExtensions
{
public static MvcHtmlString Notification(this HtmlHelper htmlHelper)
{
// Look first in ViewData
var notification = htmlHelper.ViewData["Notification"] as string;
if (string.IsNullOrEmpty(notification))
{
// Not found in ViewData, try TempData
notification = htmlHelper.ViewContext.TempData["notification"] as string;
}
// You may continue searching for a notification in Session, Request, ... if you will
if (string.IsNullOrEmpty(notification))
{
// no notification found
return MvcHtmlString.Empty;
}
return FormatNotification(notification);
}
private static MvcHtmlString FormatNotification(string message)
{
var div = new TagBuilder("div");
div.AddCssClass("notification");
div.SetInnerText(message);
return MvcHtmlString.Create(div.ToString());
}
}
然后在你的主人:
And then in your master:
<%= Html.Notification() %>
这篇关于什么是推荐的方法,在MVC提供用户通知/确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!