Datatables AJAX从连接表中获取字段

时间:2017-08-29 22:30:29

标签: javascript asp.net-mvc datatables

我正在使用JQuery DataTables和AJAX。我很难从辅助表中获取通过主键连接的字段。我能够展示的唯一的东西是主键。在这种情况下,关注_id。

模型:问题

    public int question_id { get; set; }
    public int concern_id { get; set; }
    public string question_text { get; set; }
    public string question_answer { get; set; }

模特:关注

    public int concern_id { get; set; }
    public string concern_desc { get; set; }

我希望能够展示Concer_desc。如果我不在数据表中使用AJAX,我可以轻松地获取我需要的字段。

示例:

@Html.DisplayNameFor(model => model.concern.concern_desc)

控制器:

   public JsonResult GetQuestionRecord()
    {
        bool proxyCreation = db.Configuration.ProxyCreationEnabled;
        try
        {
            db.Configuration.ProxyCreationEnabled = false;
            var list = (from q in db.questions
                        join c in db.concerns on q.concern_id equals c.concern_id
                        select q).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json(ex.Message);
        }
        finally
        {

            db.Configuration.ProxyCreationEnabled = proxyCreation;
        }

    }

查看:

<table id="DataTable" class="display" style="width:100%">
<thead>
    <tr>
        <th>ConcernID</th>
        <th>QuestionText</th>
    </tr>
</thead>
<tbody></tbody>

JavaScript:

$(document).ready(function () {
    GetQuestionRecord();
});

var GetQuestionRecord = function () {
    $.ajax({
        type: "Get",
        url: '@Url.Action("GetQuestionRecord","questions")',
        success: function (response) {
            BindDataTable(response);
        }
    });
}     
var BindDataTable = function (response) {
    $("#DataTable").DataTable({
        "aaData": response,
        "aoColumns": [
            { "mData": "concern_id" },
            { "mData": "question_text" },
        ]
    });
}

我在&#34; mData:Concer_id&#34;中有什么变化?为了显示&#34; concern_desc&#34; 。我试过了:

{ "mData": "concern_desc" }

{ "mData": "concern.concern_desc" }

1 个答案:

答案 0 :(得分:2)

步骤1:将Controller中的LINQ查询更改为此。

&#13;
&#13;
        var list = (from q in db.questions
                        join c in db.concerns on q.concern_id equals c.concern_id
                        select new
                        {
                            question_id = q.question_id,
                            concern_id = q.concern_id,
                            question_text = q.question_text,
                            question_answer = q.question_aswer,
                            concern_desc = c.concern_desc
                        }).ToList();
&#13;
&#13;
&#13;

第2步:将BindDataTable JS代码更改为。

&#13;
&#13;
var BindDataTable = function (response) {
    $("#DataTable").DataTable({
        "aaData": response,
        "aoColumns": [
            { "mData": "concern_id" },
            { "mData": "question_text" },
            { "mData": "concern_desc" }
        ]
    });
&#13;
&#13;
&#13;