返回异步对象列表为Json MVC + Knockout

时间:2017-03-02 02:35:43

标签: c# json asynchronous knockout.js

以下代码是常规控制器(不使用API​​)的一部分,我需要将多个值返回到knockout.js文件。问题是,文件正在等待接收Json,当我发送列表时,我收到一条消息说"语法错误:位于0和#34;的JSON中的意外标记S,但如果我更改签名并转换对象为JSON,然后asq的linq不运行。

我的问题是,如何使用async获取列表(因此它在淘汰赛中工作)并仍然将其作为JSON发送?或者我应该走哪条路?

public async Task<List<InsDetDTO>> LoadInsDet(int id)
    {
        var ins = await db.INSPECTIONDETAILS
            .Select(t =>
            new InsDetDTO()
            {
                Id = t.ID,
                AreaEquipment = t.AreaEquipment,
                InspectionID = t.InspectionID
            }).Where(t => t.InspectionID == id).ToListAsync();
        return ins;
    }

旁注,这是我第一次尝试使用淘汰赛,任何信息都值得赞赏,是的,我已经用谷歌搜索了一段时间。

KO

var ViewModel = function () {
var self = this;
//inspection
self.inspections = ko.observableArray();
self.error = ko.observable();

//details
self.detail = ko.observableArray();

var inspUri = '/INSPECTIONs/LoadIns';
var insDetUri = '/INSPECTIONs/LoadInsDet/';

//function ajaxHelper(uri, method, data) {
function ajaxHelper(uri, method, data) {
    self.error(''); // Clear error message
    return $.ajax({
        type: method,
        //url: uri,
        url: uri,
        //async: false,
        dataType: 'json',
        contentType: 'application/json',
        data: data ? JSON.stringify(data) : null
    }).fail(function (jqXHR, textStatus, errorThrown) {
        self.error(errorThrown);
    });
}

function getAllInspections() {
    ajaxHelper(inspUri, 'GET').done(function (data) {
        self.inspections(data);
    });
}

self.getInspectionDetails = function (item) {
    ajaxHelper(insDetUri + item.Id, 'GET').done(function (data) {
        self.detail(data);
    });
}

// Fetch the initial data.
getAllInspections();
};

ko.applyBindings(new ViewModel());

查看                                                                                                                                              检查                                                                                                

  •                                 的                                                                  的                                                                  细节                             
  •                                                                                

                

            <!-- ko if:detail() -->
    
            <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2 class="panel-title">Detail</h2>
                    </div>
                    <table class="table">
                        <tr><td>AreaEquipment</td><td data-bind="text: detail().AreaEquipment"></td></tr>
                        <tr><td>InspectionID</td><td data-bind="text: detail().InspectionID"></td></tr>
                    </table>
                </div>
            </div>
    
            <!-- /ko -->
    

    1 个答案:

    答案 0 :(得分:0)

    玩了一下之后,我发现我的问题,在控制器中,如果我先将结果更改为List,那么当我转换为JSON时,它更容易在视图中捕获它们,这些是我的改变,希望这将有助于某人。

    查看请注意,点击:$ parent.getInspectionDetails填充KO代码中的详细信息,因此我可以使用foreach获取所有详细信息

    <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2 class="panel-title">Inspections</h2>
                    </div>
                    <div class="panel-body">
                        <ul class="list-unstyled" data-bind="foreach: inspections">
                            <li>
                                <strong><span data-bind="text: Id"></span></strong>
                                <span data-bind="text: date"></span>
                                <strong><span data-bind="text: labDesc"></span></strong>
                                <span data-bind="text: username"></span>
                                <small><a href="#" data-bind="click: $parent.getInspectionDetails">Details</a></small>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="alert alert-danger" data-bind="visible: error"><p data-bind="text: error"></p></div>
            </div>
    <!-- ko if:detail() -->
            <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2 class="panel-title">Detail</h2>
                    </div>
                    <table class="table">
                        <tr class="list-unstyled" data-bind="foreach: detail">
                            <td>AreaEquipment</td><td data-bind="text: AreaEquip"></td>
                            <td>InspectionID</td><td data-bind="text: Inspectionid"></td>
                        </tr>
                    </table>
                </div>
            </div>
    

    控制器(请注意ToList())

    public JsonResult LoadInsDet(int id)
        {
            var ins = from t in db.INSPECTIONDETAILS.Where(t=>t.InspectionID==id).ToList()
                      //where t.InspectionID == id 
                      select new InsDetDTO()
                      {
                          Id = t.ID,
                          AreaEquip = t.AreaEquipment,
                          Inspectionid = t.InspectionID
                      };
            return Json(ins, JsonRequestBehavior.AllowGet);
        }