views:

17

answers:

1

I'm using the jQuery NotifyBar quite nicely in an Index view to display business rule errors when a user e.g. clicks a delete link for an item than cannot be deleted. However, if the user adds a new item, they are redirected to the Create view. If the new item is successfully created, the Create action redirects back to the Index view.

My quandary is that I need (have been told) to show a success notification in the above scenario. Previously, to request a notification while remaining on the same view, I was using return JavaScript() for an action result, but when I use return RedirectAction() for the action result, I'm left with nowhere to put the return JavaScript().

The way I see this is that I need to: a) include information in the return RedirectAction() that tells the 'destination' view to show the notification, or b) invoke the notification in the 'source' view, instead of the return RedirectAction(), and tell it that when it closes/is closed, to perform the redirect to the 'destination' view.

I have no idea where to begin deciding between these two opetions, nor how to even begin researching how to implement either. All advicem and pointers to advice will be most appreciated.

+1  A: 

I like option A the best. You could easily include a querystring value with the return url and have a javascript function waiting on the return page that looks for the querystring value... if present, show notification bar.

Submit action on controller:

public ActionResult Submit(ValueModel valueModel) {

    //TODO: Save model to repository

    //include something in the route values to act as a querystring flag.
    //here, I use "success". 
    return RedirectToAction("Action", "Controller", new { success = 1 });
}

View action on controller:

public ViewResult Index() {

    //TODO: do stuff

    return View();
}

Index.aspx:

...

<div class='notificationBar'></div>

<script type="text/javascript">
    $(document).ready(function() {
        if(window.location.search.substring(1).indexOf("success")) {
            //set notification bar here
        }
    });
</script>

...
Byron Sommardahl
@Byron, Thanks for the vote of confidence in my option of choice as well, but any clue as to how I include a querystring value would be great.
ProfK
I added a comment to the submit method code.
Byron Sommardahl
Thanks. Now I need to figure out how to get that script into my view, which has a master page. I.e., Index.apcx only has content placeholders.
ProfK
Drop it anywhere. My favorite place to put javascript blocks is at the bottom of my html document or as close to the bottom as I can get it. That way it loads last and the page content comes up as quickly as possible.
Byron Sommardahl