Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 905091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:09:29+00:00 2026-05-15T16:09:29+00:00

A common scenario I encounter is providing notifications / confirmations to users after they

  • 0

A common scenario I encounter is providing notifications / confirmations to users after they have performed an action to inform them of success.

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

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?

Option 1: Have an entirely separate View

This seems to violate DRY principles by having an entirely new View (and ViewModel) to display almost identical information, expect for the user notifcation.

Option 2: Conditional Logic in the View

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&#33;</div>
   <% } %>

Option 3: Use jQuery to check for TempData on page load

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:

  • 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.

What say you? Option 1,2,3 or none? Is there a better way?

Please augment my coding patterns!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:09:30+00:00Added an answer on May 15, 2026 at 4:09 pm

    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>
    

    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() %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have this common scenario where we have a method that performs some action
Common scenario: I have a library that uses other libraries. For example, a math
This is a very common scenario: displaying images in a ListView which have to
So I have a common scenario where everything depends on AJAX responses, followed by
I have a ASP.NET MVC 3 app with a common scenario where there is
Present Scenario : I have a set of classes that all take a common
I think my scenario is pretty common. I have a database and I want
My common scenario: I have a web app and a test project in the
I think this is a pretty common scenario: I have a webpage that's returning
I have a, what seems to be, rather common scenario I'm trying to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.