Search code examples
javascriptasp.netknockout.jsknockout-mapping-plugin

How can I make Knockout.Mapping working in ASP.NET Web App?


I'm constructing a MVC5 Web App and want to make a dynamic page view by using Knockout.js. However, I have found that Knockout.Mapping doesn't seem to work correctly in my project.

As you see Intellisense does not the suggestion for the mapping plugin (I have included the reference of the plugin in the _reference.js). And it fails to show the second alert written when I complete this line forcibly.

Do I have to do something more to make it work correctly?

@if (false)
{
<script src = "~/Scripts/knockout-3.4.0.js" ></script >
<script src = "~/Scripts/knockout.mapping-latest.js"></script >
}

<script type="text/javascript" src="@Url.Content("~/Scripts/knockout-3.4.0.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/knockout.mapping-latest.js")"></script>

//~~body~~//

<script type="text/javascript">
alert("1");
var json = '@Html.Raw(Json.Encode(Model))';
b = ko.mapping.fromJson(json); //"mapping" is not suggested when "ko." is put.
//b = ko.mapping.fromJson(Model); //mistake at the original post
ko.applyBindings(b);
alert("2"); // not showed when the previous two lines is active.
</script>

Intellisense is not working


Solution

  • Sorry for delaying follow-up. Finally I make it working by using

    <script src="~/Scripts/knockout-3.4.0.js"></script>
    <script src="~/Scripts/knockout.mapping-latest.js"></script>    
    
    //~~body~~//
    
    @section scripts{
            <script src="~/Scripts/knockout-3.4.0.js"></script>
            <script src="~/Scripts/knockout.mapping-latest.js"></script>
    }
    

    and show the suggest by putting

    /// <reference path="knockout.mapping-latest.js" />
    

    after the reference of the core library of knockout.js in _reference.js.