MVC 5 Using spa template cannot display views

I had a problem displaying my entire view. (no js error) No matter what, when I go to groceries or at home, both kinds are displayed, and I don’t understand why. Isn't that ko: is it supposed to limit one or the other kind? What am I missing?

Index.cshtml

@section SPAViews {
    @Html.Partial("_Home")
    @Html.Partial("_Products")
}
@section Scripts{
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/app")
}

_Home.cshtml

<!-- ko with: home -->
...
<!-- /ko -->

_Products.cshtml

<!-- ko with: products -->
...
<!-- /ko -->

products.viewmodel.js

function ProductsViewModel(app, dataModel) {
    var self = this;

    self.query = ko.observable();

    Sammy(function () {
        this.get('#products', function () {
            $.ajax({
                method: 'get',
                url: app.dataModel.productsUrl,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                    self.query(data.query);

                }
            });
        })
    });

    return self;
}

app.addViewModel({
    name: "Products",
    bindingMemberName: "products",
    factory: ProductsViewModel
});

home.viewmodel.js

function HomeViewModel(app, dataModel) {
    var self = this;

    Sammy(function () {
        this.get('#home', function () {
            console.log('home');
        });
        this.get('/', function () { this.app.runRoute('get', '#home') });
    });

    return self;
}

app.addViewModel({
    name: "Home",
    bindingMemberName: "home",
    factory: HomeViewModel
});

Ligaments:

 bundles.Add(new ScriptBundle("~/bundles/app").Include(
        "~/Scripts/sammy-{version}.js",
        "~/Scripts/app/common.js",
        "~/Scripts/app/app.datamodel.js",
        "~/Scripts/app/app.viewmodel.js",
        "~/Scripts/app/home.viewmodel.js",
        "~/Scripts/app/products.viewmodel.js",
        "~/Scripts/app/_run.js"));
+4
source share
1 answer

This may be due to a misunderstanding of the binding with. From the docs :

, - .

, . - .

, if ( ifnot), :

if , ( ), true...

, if , .

0

Source: https://habr.com/ru/post/1548547/


All Articles