C # property value doesn't change in Javascript / jquery

I initialize the property inside the controller constructor.

public BookController() { SessionProvider.SessionLoadSceanrio = false; } 

I have an action method that reset again contains a button click event property.

 public ActionResult LoadScenario(int bookId) { SessionProvider.SessionLoadSceanrio = true; // remaining code return Json(ScenarioId, JsonRequestBehavior.AllowGet); } 

The 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, the value of the property changes in the controller. But this does not change in my javascript code. Please view the screen capture tool for developers.

ScreenCapture

Does anyone have a clue to this?

+6
source share
2 answers

Shaver syntax is used. then the page is initially loaded, the values ​​are set, and these values ​​are a string, not a variable. then you cannot refresh the session value again.

please get the session value from an AJAX call and set for your variable.

+4
source

The code below will only be evaluated after receiving the submission from the server and will not be re-evaluated after your ajax call.

 var isLoadScenario = "@Portal.Presentation.Web.Planning.MVC.App_Start.SessionProvider.SessionLoadSceanrio"; 

To do what you intended, you will need to return SessionLoadSceanrio in your answer.

You can do it like:

  public ActionResult LoadScenario(int bookId) { SessionProvider.SessionLoadSceanrio = true; // remaining code return Json(new {ScenarioId, SessionLoadSceanrio = SessionProvider.SessionLoadSceanrio}, JsonRequestBehavior.AllowGet); } 
+3
source

All Articles