I'm new to asp 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?
I'm new to asp 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?
Share Improve this question asked Oct 5, 2013 at 19:47 Alaa ShehebarAlaa Shehebar 811 gold badge2 silver badges14 bronze badges 1- Possible duplicate of Asp mvc passing a C# object to Javascript – Liam Commented Jul 25, 2017 at 8:30
1 Answer
Reset to default 14Just 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