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))
return View("Index");
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!</div>
<% } %>
3: jQuery TempData
, TempData SubmitFeedback Action. jQuery . :
<%=Html.Hidden("HiddenField", TempData["UserNotification"])%>
$(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 ? ?
, !