0

I have this jquery function placed in my .js file.... As long as my page is loaded this works... but when i changed it to return as a partial view.... this alert wont work anymore... How is it gonna be working again ?

  $(document).ready(function () {

    var msg = '@ViewBag.Message';

    if (msg == '1')
        alert("New Time Shift has been saved.");

});

In my controller action...

   if (Request.IsAjaxRequest())
          return PartialView("_RecordList", userRecord); //alert wont work here...

   return View(userRecord); //this will return the whole view thus the alert works here
4
  • Where is the js page included (In partial view/view) ? Commented Nov 19, 2012 at 11:23
  • how you request the View?what is included in view? ... all is important to answer this question. Commented Nov 19, 2012 at 11:41
  • try look into firebug or some debug console of your browser to see if you js is actually loaded (or just simply look into generated HTML if reference to your js is included) Commented Nov 19, 2012 at 11:45
  • Have u included the JS file in ur partial view? if not, then add it into ur partial view, and I hope it will return you the alert Commented Nov 19, 2012 at 11:49

2 Answers 2

1

The document ready function will not be executed from ajax requests. You could extract the javascript code to a separated function part of the whole view. You can also create a callback function to be executed when the partial request succeeds, that will also call it:

$(document).ready(function () {
    var msg = '@ViewBag.Message';
    initFunction(msg);
});

function initFunction(msg){
    if (msg == '1')
        alert("New Time Shift has been saved.");
}

function partialRequestSuccess(data){
    //store the message somewhere in the partial view, like a hidden div and get it using jquery
    var msg = ...
    initFunction(msg);
}

Then you could set the complete callback of the ajax request to call the success callback we have just created. If you are using the MVC ajax helpers, there is a Success parameter that you can set like:

@using(Ajax.BeginForm(new AjaxOptions{ OnSuccess = "partialRequestSuccess" }))
Sign up to request clarification or add additional context in comments.

3 Comments

this is right... but my @ViewBag.Message does not have the updated data set in my controller....
You are right about the viewVag. Just edited my answer, so you will need to include that message somewhere in the partial view. Then on the ajax callback you can recover it and pass it to that initfunction. If you return a JSON from the controller action then it is even easier, as you could just get the message from the received JSON.
thx for giving me more ideas...especially about the AjaxOptions parameters :)
0

I have this jquery function placed in my .js file....

var msg = '@ViewBag.Message';

This is server code, that works only in *.cshtml files, this is minimum one problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.