6

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.

ScreenCapture

does Anyone has a clue on this?

6
  • I see there's one error in console. What does it say? Commented Jul 10, 2015 at 7:00
  • The code which you have pasted is different from screenshot. Are you sure you are referring to the same code while running it? Commented Jul 10, 2015 at 7:00
  • thushar .. it's css error.. we can ignore it.. Commented Jul 10, 2015 at 7:01
  • Mike its the same code. The developer tool shows with value. that's what I think it appears different.And I didn't list here some properties to increase readability. But it's actually there in the code Commented Jul 10, 2015 at 7:02
  • 2
    Reloading the page. I did that. But same result. Commented Jul 10, 2015 at 8:22

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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);
    }

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.