MVC中的WebApi和Knockout模式

时间:2012-12-05 10:58:52

标签: asp.net-mvc design-patterns knockout.js asp.net-web-api

我遇到了将我的应用程序带入WebApi + Knockout世界所需的模式问题。以下情况已经引发了一些。

我有一个非常基本的网格局部视图(_grid.cshtml)。它使用GridViewModel,它只有一个QueryID。 Grid部分使用Ajax(在ko ViewModel中定义)将QueryID传递给WebApi方法,该方法返回JSON中的对象集合。然后我使用knockout根据返回的JSON绑定网格的列,然后加载行。

这一切都很好,但我不确定这是否是正确的方法。

    // When the query is changed update the grid data.
    self.selectedQueryID.subscribe(function (newQuery) {
        self.SelectQuery(newQuery.ID());
    });

    // Execute the query and set the results as the rows of the table
    self.SelectQuery = function (queryID) {
        $.ajax({
            url: '/api/Query/Execute?ID=' + queryID,
            type: 'POST',
            contenttype: 'application/json',
            success: function (result) {
                self.gridData(result);
            }
        });
    };

当我有另一个部分视图时,复杂性就出现了,该视图是用户可以选择的可用查询列表。该局部视图位于网格局部视图附近。我希望能够以某种方式将点击的查询发送到网格,以便它可以再拍摄另一个ajax请求以获取新查询的数据。我知道这是考虑MVC应用程序的错误方法,但我对WebApi不够了解。

    @Html.Partial("~/Views/Shared/_Grid.cshtml", Model.GridViewModel)
    @Html.Partial("~/Views/Shared/_Queries.cshtml", Model.User)

这是为网格实现某种默认查询,然后为用户提供选择其他查询的选项。

我也开始研究Backbone和Knockback,但是如果这是正确的方向,我们不知道(足够他们或者)。

0 个答案:

没有答案
相关问题