3

I have been working with MVC a little while now (2 months) and am loving it for the most part. I have come across a few instances where I cannot share values of variables between languages. For example.

In javascript/Jquery and so on I can set cookies. But the server cant get at them.

In C# / Razor I can set Query strings, ViewBags, ViewData, Sessions ect. But because this is server side, clientside javascript cant get at them.

What I want to know is, if there is a reasonably simple way I can set a variable like a string in C# or javascript, and read it either pre or post back (doesnt matter) in the other language (ie, a c# Razor variable read by Javascript, or vice versa).

I have not had need to use Json or XML yet with AJax. But if this would help I would consider it. I am new to all this so please forgive my ignorance and thank you for your help.

1
  • Why do youbsaid that you will not able to get cookie server side? It's possible. Commented Jul 3, 2012 at 18:14

4 Answers 4

6

C# -> JS

Put variables in hidden input fields or data-* attributes and then just Javascript or jQuery to retrieve those values.

JS -> C#

Either use ajax to send stuff back or you could set the values of hidden inputs and then just do a normal form POST to the server.

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

2 Comments

So essentially the only easy way common to both is to use a hidden input field. Interesting. I taught this was considered bad practice that is why I never did it before. Thanks for your feedback.
Hidden input fields are designed to provide data transfer that the user does not need to interact with. Just be aware, and maybe this is why you thought it was bad practice, that the user will be capable of modifying any data you store on the page. Therefore you can never trust the hidden input data and you will need to treat it as if the user typed it themselves.
2

If hidden fields solve your problem then IMHO I'd say this is probably the simplest way to go. If the JS must read the data, and you don't want it visible, then hidden fields make sense.

However, I've had the need to have some fairly complex data elements, say that come from a DB, that need to be initialized in JS in a specific way - for example: an event calendar. The event data is read from a DB, but must somehow make it into JS data assignments.

I found the most direct way was to @Html.Raw("whatever you need") them in a loop in the view so that the View is outputting Javascript - sounds klunky but I only use it for dynamic data output. There might be a cleaner way to do this (happy to read suggestions) but it's an option for more complex data delivery to JS.

Incidentally, the data gets back to the server because the URL's are crafted with querystrings to do this (for lookup data only where security concerns are low).

Comments

2

There is one more solution. You can use NewtonsoftJson to serialize entire model, or just some part of model or any other server variable to a json object:

Javascript & Razor:

var model = @Html.Raw(JsonConvert.SerializeObject(Model))

The issue is that using this approach you probably will get syntax error because of silliness of VS2012 and Resharper. But there are some tricks: Razor/JavaScript and trailing semicolon.

1 Comment

Thank you for your answer. I'll have to spend a bit of time looking at it in the future because I have moved on since. However, Thanks for taking the time to look and post an answer.
1

If it's complicated you may want to look into KnockoutJs.com - it handles serialization of the viewmodel to and from javascript and takes a lot of work off your plate.

1 Comment

Thank you for your answer. I have now over a year experience with MVC and resolved the problem. However, I do like your suggestion to use KnockoutJs. Over the last year I have gone the route of complete separation of concerns where my views contain no code / css instead its all linked from outside files. As such I am not a fan of KnockoutJs anymore as I feel it bloats the html/razor too much. However that could just be because I lack experience using knockout. Presently it just doesn't fit my style. But as mentioned, thank you for your answer and it is a good one.

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.