Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Success Alert popup in mvc4 View without using jquery and AJAX

Is it possible to get an alert popup on return to same view after success in mvc4 without using ajax begin form?

I'm trying to submit a form and on success want to show a alert box without using ajax and jquery .

like image 642
Joseph Avatar asked Nov 29 '13 06:11

Joseph


1 Answers

When you submit form, I think then you are redirecting, am i right? So you can use TempData for this purpose:

In controller action:

if(success)
{
    TempData["AlertMessage"] = "my alert message";
    return RedirectToAction("SomeAction");
}

The view which SomeAction action returns (or in layout view):

@{
    var message = TempData["AlertMessage"] ?? string.Empty;
}

<script type="text/javascript">
    var message = '@message';
    if(message)
        alert(message);
</script>

NOTE: If you are not redirecting, but returning view, just use ViewBag instead of TempData.

like image 68
karaxuna Avatar answered Oct 13 '22 20:10

karaxuna