Transfer view using javascript code to seprate js file in mvc

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")" ...)
   </script>

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

Please solve this problem as soon as

Thanks in advance

Last updated:5/26/2015 7:53:43 AM

1 Answers

Anonymous User
Anonymous User

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));
    DoThis(model);
</script>

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.

Answer