在MVC中提供用户通知/确认的推荐方法是什么?

时间:2010-07-09 12:55:06

标签: jquery design-patterns asp.net-mvc-2

我遇到的常见情况是在用户执行操作后向用户提供通知/确认信息,以告知用户成功。

例如,假设用户提供反馈表单的反馈,然后点击提交反馈。您可能需要显示“感谢您的反馈”消息 之后您已执行了一些验证,例如他们在数据库中有一个有效的电子邮件。一些伪代码:

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还是在我的Action早期(即在我开始访问数据库之前)和如果有无效的条目,请退出我的操作。

但是,所有条目都有效并且您想要显示确认消息的情况呢?

选项1 拥有完全独立的视图

这似乎违反了DRY原则,通过使用全新的View(和ViewModel)来显示几乎相同的信息,期望用户通知。

选项2 视图中的条件逻辑

在这种情况下,我可以在View中使用条件语句来检查是否存在SubmitFeedback Action中传递的某些TempData。再次,伪代码:

   <% if(TempData["UserNotification"] != null {%>
   <div class="notification">Thanks for your Feedback&#33;</div>
   <% } %>

选项3 使用jQuery检查页面加载时的TempData

在这种情况下,我将有一个隐藏字段,我将通过SubmitFeedback操作填充TempData。然后我会使用jQuery来检查隐藏的字段值。更多伪代码:

<%=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);
});

我对此的初步想法是:

  • 选项1:视图完全分离但看起来有点过分且效率低下(违反DRY)
  • 选项2:很简单,但在视图中有条件逻辑(我不会因为这个而牺牲MVC祭坛吗?!)
  • 选项3:感觉它过于复杂。它确实避免了View中的逻辑。

你说什么?选项1,2,3或没有?有没有更好的办法?

请扩充我的编码模式!

2 个答案:

答案 0 :(得分:6)

我喜欢选项1.此外,您不需要条件,它适用于禁用的JavaScript。只需将其粘贴在母版页的某个位置即可:

<div class="notification">
    <%= Html.Encode(TempData["Notification"]) %>
</div>

您当然可以通过使用一些不错的插件(例如jGrowlGritter或甚至查看how StackOverflow does it来逐步增强/设置动画。

另一个解决方案是编写一个可能是最好的帮助器:

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());
    }

}

然后在你的主人:

<%= Html.Notification() %>

答案 1 :(得分:1)

对于一般通知内容,我通常创建一个局部视图来隐藏丑陋的条件代码,并将其包含在我的主布局中。

所以,在主人的适当位置:

<% Html.RenderPartial("_Notifications") %>

在局部视图中:

<% if(TempData["UserNotification"] != null {%>
    <div class="notification">Thanks for your Feedback&#33;</div>
<% } %>