Reputation: 1316
I initialize a property within the Controller's Constructor.
public BookController()
{
SessionProvider.SessionLoadSceanrio = false;
}
I have an Action Method which reset the property again on a button click event.
public ActionResult LoadScenario(int bookId)
{
SessionProvider.SessionLoadSceanrio = true;
// remaining code
return Json(ScenarioId, JsonRequestBehavior.AllowGet);
}
Following javascript code is in my view which is called when the button is clicked.
var BookHandler = {
$("#btnLoadScen").click(function (e) {
$.ajax({
url: "@Url.Action("LoadScenario", "Book")",
dataType: 'json',
type: 'POST',
data: {
'bookId': BookHandler.getBookId()
},
success: function (response) {
var scenarioId = response;
var isLoadScenario = "@Portal.Presentation.Web.Planning.MVC.App_Start.SessionProvider.SessionLoadSceanrio";
//otherproperties
window.open("@Url.Action("Index", "BookScenario", new {loadScenario = "_loadScenario"}).replace('_loadScenario', isLoadScenario), tabId);
},
error: function () {
}
});
});
}
My problem is when I click the button, value of property changes in the Controller. But it doesn't change in my javascript code.Please see the screen capture of the developer tool.
does Anyone has a clue on this?
Upvotes: 6
Views: 340
Reputation: 2106
You are using the razor syntax. then initially page loaded , values are set, and these values are string , not a variable anymore. then you can't get session value updated again.
please get session value from an AJAX call and set to your variable.
Upvotes: 3
Reputation: 4570
The code below will only be evaluated once you get the view from server and will not be re-evaluated after your ajax call.
var isLoadScenario = "@Portal.Presentation.Web.Planning.MVC.App_Start.SessionProvider.SessionLoadSceanrio";
In order to do what you intended, you will need to return SessionLoadSceanrio in your response.
You can do it like:
public ActionResult LoadScenario(int bookId)
{
SessionProvider.SessionLoadSceanrio = true;
// remaining code
return Json(new {ScenarioId, SessionLoadSceanrio = SessionProvider.SessionLoadSceanrio}, JsonRequestBehavior.AllowGet);
}
Upvotes: 3