asp.net-mvc-4asp.net-web-apiupshot

Call Web api method with parameters through Html.UpshotContext


I am currently working on MVC4 SinglePage Application.

I have a Web Api method GetChartsByCategory(int catId) So in my view cshtml page How shall I declare Html.UpshotContext in this scenario.

I dont want to call GetAllCharts() and then filter on client side using knock out or upshot.

Thanks


Solution

  • It is not possible to supply parameters, using Html.UpshotContext. You could call GetChartsByCategory() using $.ajax() and mapping the results to your model.

    Example:

    $.ajax("GetChartsByCategory", true, {
        data: { id: catID },
        dataType: 'json',
        success: function (data) {
            // on success, data contains a list of charts
            self.charts(ko.utils.arrayMap(data, function (item) {
                return new Chart(item);
            }));
        }
    });
    

    Model:

    Chart = function (initialData) {
        var self = this;
    
        // inject the initial data
        $.each(initialData, function (key, value) {
            self[key] = ko.observable(value);
        });
    
    ....
    }