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!</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!