Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to access C# Static Variable in JavaScript/JQuery in an MVC 4 application. I am able to access it with following syntax.

var currentCulture = '@StaticCache.Culture';  //Returns en-GB

Issue is that once page load I can access correct value but assuming we make one Ajax Call to server and that call (in Controller) change the value for static variable.

StaticCache.Culture="en-US";

Again read same static variable in Java Script (Razor View)

var currentCulture = '@StaticCache.Culture';  //**Still returns en-GB instead of en-US**

It cached the variable somehow. Is there any way I can read the latest value?

Thank you Best Regards.

share|improve this question
    
You can use "Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");" to save language. –  Jhoon Bey Feb 24 at 11:16
1  
Static variables on a Web Application is NEVER a good idea, you should make it a Session variable rather, or depending on a couple of things, you can use MVC's ViewBag. –  jacqijvv Feb 24 at 11:16
    
Even Session Variable getting cached in View. I replaced Static Variable with Session it updates value in C# (Controller) but does not update it in View when accessing through Async calls. With entire page reload both the approaches works. –  user2739418 Feb 24 at 11:41
add comment

1 Answer 1

up vote 0 down vote accepted

If you're operating on AJAX, and something is being updated that needs to be reflected on the page, then you should return it in the response from your AJAX request.

Using that Razor syntax is only going to update those values when loaded from the server, as you've discovered. There's no way client side to know that the value has changed server side without the two communicating.

If you don't want to partially reload the page, or return it in the AJAX response, you can look at libraries like SignalR which keep a real-time communication channel available between the client and server (which will update your values via JavaScript once implemented.)

share|improve this answer
    
Thanks CLandry. That's what I did. Re-Loading option we already ruled out as it was making some part of Dashboard quite bulky. –  user2739418 Feb 24 at 15:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.