TRANSFER VIEW USING JAVASCRIPT CODE TO SEPRATE JS FILE IN MVC

Alex Leblois

Total Post:67

Points:471
Posted by  Alex Leblois
 819  View(s)
Ratings:
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")" ...)

   </script>

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

Please solve this problem as soon as

Thanks in advance

  1. Pawan Shukla

    Post:29

    Points:203
    Re: Transfer view using javascript code to seprate js file in mvc

    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.

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!