Monday, May 20, 2024
85
rated 0 times [  90] [ 5]  / answers: 1 / hits: 15355  / 11 Years ago, sat, october 5, 2013, 12:00:00

I'm new to asp.net MVC4, please be patient.



I have a controller which returns ActionResult with a model.
I can use this model through Razor but I'd like to use it in JavaScript.
this what I've tried to do according to some answers on the internet:



@{
var property = Model;
}
var prop = @(property);
var data= @Html.Raw(Json.Encode(prop ));


But it's not working.
What do I need to do in order to pass my model to JavaScript?


More From » asp.net-mvc-4

 Answers
17

Just use



 <script>
var data = @Html.Raw(Json.Encode(Model));
</script>


Problem with Your Code



@{
var property = Model;
}
var prop = @(property); //Here model is not properly encoded
var data= @Html.Raw(Json.Encode(prop )); //You are passing Javascript variable

[#75202] Friday, October 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
prestonh

Total Points: 384
Total Questions: 105
Total Answers: 105

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;