How to send value from model in view to javascript file?


In asp.net MVC, How can I send value from model in view to javascript file? 


Asked by:- DanielNguyen
1
: 3771 At:- 7/19/2017 7:07:10 AM
MVC asp.net-mvc javascript

Daniel, can you elaborate your question? With some code or more details 0
By : pika - at :- 7/19/2017 8:06:22 AM






1 Answers
profileImage Answered by:- vikas_jk

I assume you have a javascript file with a function

function myFunc(value) {
   var x= value;
 }

to send value from Model to this function you need to create a script in view to call it

<script>
   myFunc(@Model.Value);
</script>

Or another method to send value from model to javascript file is to directly convert the model to JSON object, then you can use it in your js file. The code as below:

var object = @(Html.Raw(Json.Encode(Model)));

That's it, hope it will help you, please upvote the answer and mark it as answer (if it works for you, and you have two votes for answer)

2
At:- 7/19/2017 10:44:18 AM Updated at:- 7/19/2017 10:53:41 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use