What is the recommended approach for providing custom notifications / confirmations in MVC?

The common scenario that I come across is to provide notifications / confirmations to users after they have completed an action to inform them of success.

For example, suppose a user pays attention to a feedback form, and then clicks Send Feedback . You might want to display a "Thank you for your feedback" message after you have performed some verification, for example. they have a valid email address in the database. Some pseudo codes:

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

I understand how to check records with Html.ValidationMessage, etc. This is normal, and I usually check for invalid records either using jQuery on the client side or at the beginning of the action (i.e. before I start deleting the database) and exit mine if there are invalid records.

However, what about a scenario in which all entries are valid and you want to display a confirmation message?

Option 1 : Has a completely separate view

This seems to violate DRY principles by having a completely new View (and ViewModel) to display almost identical information while waiting for the user to be notified.

Option 2 : Conditional Logic in the View

, TempData, SubmitFeedback Action. , :

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

3: jQuery TempData

, TempData SubmitFeedback Action. 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: , . , .

? 1,2,3 ? ?

, !

+3
2

1. , javascript. - , :

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

/ , - , jGrowl Gritter fooobar.com/questions/120401/....

- , , , :

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() %>
+6

, .

, :

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

:

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

Source: https://habr.com/ru/post/1753880/


All Articles