I am using a HtmlHelper to display an update to a user as follows:
In webpage:
<%=Html.CourseUpdateNotification() %>
In my controller:
public ActionResult UpdateOnceOnlyCourseRecord(some parameters)
{
//validation code etc...
//Save to database etc...
TempData["CourseUpdateNotification"] = "Success";
return RedirectToAction("ShowCourses", "Course");
}
At the moment, the notification to the user remains on the screen until the user navigates away from the page or refreshes the page (as expected).
The effect I would like to achieve is to display the notification but then fade it out after 3 seconds.
I tried to achieve this with the following jQuery:
(NOTE – The HtmlHelper uses TagBuilder to create a div with the class attribute feedback-complete)
$(document).ready(function() {
$(function() {
if ($('div.feedback-complete').length > 0) //Check if exists
{
setTimeout(function() { $('div.feedback-complete').fadeOut(); }, 3000);
}
});
});
Unfortunately this does not work for me and I cannot work out why. I have tried a few variations, including $(window).load, etc. but to no avail.
Am I missing something more fundamental regarding HtmlHelper and how it can be accessed after the page load?
Insights always appreciated.
After some research I think the issue was how the return string from my
HtmlHelperwas formatted.Initially I had this in the
HtmlHelper:Which rendered like this in Html (note the lack of apostrophes around the class name):
Instead, I changed the
HtmlHelperto format the string as follows:This then rendered Html that had
'around the class name…Once I changed the above I got the fade-out effect I was looking for after a 3 second delay using the following jQuery (Hat tip to
Charlino)Finally, I find it interesting that the the lack of apostrophes around the class name did not stop the CSS from rendering the Html correctly. I (naively) assumed at the outset that the fact that the html fragment rendered correctly without the apostrophes meant that this couldn’t be the issue of why the jQuery fadeout effect wasn’t working for me…