Home > DeveloperSection > Forums > Transfer view using javascript code to seprate js file in mvc
Alex Leblois
Alex Leblois

Total Post:67

Posted on    May-26-2015 7:30 AM

 JavaScript Mvc  Knockout.js 

 1 Reply(s)
 453  View(s)
Rate this:

I’m using knockout.js in my asp.net mvc project I have javascript code

<script type="text/javascript">

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

    $.get("@Url.Action("_CityPartial")" ...)


How to transfer this javascript code in a separate js file.

Please solve this problem as soon as

Thanks in advance

Pawan Shukla
Pawan Shukla

Total Post:29

Posted on    May-26-2015 7:53 AM

You can create external .js file with your code in and pass your serialized json object to it like this:

<script type="text/javascript">

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



This has the benefits of keeping the main body of javascript in a separate file.


Any other razor variables can be passed across to the methods defined in the javascript in the same manor as the model has been above.

Modified On Mar-17-2016 04:28:13 AM

Don't want to miss updates? Please click the below button!

Follow MindStick