How to pass a value to razor variable from javascript variable, is it possible asp.net mvc razor view engine?
@{
int a = 0;
}
<script>
var b = ...
@a = b;
</script>
How to pass a value to razor variable from javascript variable, is it possible asp.net mvc razor view engine?
@{
int a = 0;
}
<script>
var b = ...
@a = b;
</script>
Razor View Server Side variable can be read to Client Side JavaScript using @ While and JavaScript client side variable can read to Razor View using @:
I see that this problem was discussed some time ago, but if anyone 'll meet with this again, here is my solution:
In your *.cshtml View file:
JavaScript variable model is something that I need to pass to Razor ViewModel. It can be done with ajax request. You just need to have proper argument/s in your action, that matches Json object created in JavaScript.
Hope it'll help someone!
Step: 1 Your Html, First Store the value in your localstorage using javascript then add the line like below ,this is where you going to display the value in your html, my example is based on boostrap :
Step:2 Javascript
You can't. and the reason is that they do not "live" in the same time. The Razor variables are "Server side variables" and they don't exist anymore after the page was sent to the "Client side".
When the server get a request for a view, it creates the view with only HTML, CSS and Javascript code. No C# code is left, it's all get "translated" to the client side languages.
The Javascript code DO exist when the view is still on the server, but it's meaningless and will be executed by the browser only (Client side again).
This is why you can use Razor variables to change the HTML and Javascript but not vice versa. Try to look at your page source code (CTRL+U in most browsers), there will be no sign of C# code there.
In short:
The server get a request.
The server creates "takes" the view, compute and translate all the C# code that was embedded in the view, to CSS,Javascript, and HTML.
Okay, so this question is old... but I wanted to do something similar and I found a solution that works for me. Maybe it might help someone else.
I have a
List<QuestionType>
that I fill a drop down with. I want to put that selection into theQuestionType
property on theQuestion
object that I'm creating in the form. I'm usingKnockout.js
for the select binding. This sets theself.QuestionType
knockout observable property to aQuestionType
object when the user selects one.I have a hidden field that will hold this object:
In the subscription for the observable, I set the hidden field to a
JSON.stringify
-ed version of the object.In the
Question
object, I have a field calledQuestionTypeJson
that is filled when the user selects a question type. I use this field to get theQuestionType
in theQuestion
object like this:So if the
QuestionTypeJson
field contains something, it will deserialize that and use it forQuestionType
, otherwise it'll just use what is in the backing field.I have essentially 'passed' a JavaScript object to my model without using
Razor
or anAjax
call. You can probably do something similar to this without usingKnockout.js
, but that's what I'm using so...But it would be possible if one were used in place of the variable in @html.Hidden field. As in this example.
set the field per script:
I hope I can at least offer no small Workaround.