MVC Model Binder不识别JSON对象

时间:2014-03-26 17:22:30

标签: jquery ajax asp.net-mvc json model-binding

我将一个JSON对象从一个AJAX调用传递给一个MVC控制器Action,其中一个对象应该与JSON匹配,作为传递的参数。但是对象的所有属性都是空的,模型绑定器似乎没有做它的工作,尽管我找不到任何差异。控制器参数中的ViewModel如下...

public ActionResult ExportPatientDetailsToExcel(CaseReviewExcelTable tbl)
{
}


 public class CaseReviewExcelTable
    {
        public Guid CaseID { get; set; }
        public string Header { get; set; }
        public IEnumerable<Row> Rows { get; set; }
    }

    public class Row
    {
        public IEnumerable<Item> Items { get; set; }
    }

    public class Item
    {
        public string Label;
        public string Value;
    }

...根据Fiddler的说法,传递的JSON对象具有这种结构......

-JSON
    -CaseReviewExcelTable
        -CaseID = '<correct GUID value here>'
        -Header = '<header string here>'
        -Rows 
            -{}
                -Row
                    -Items
                         -{}
                             -Item
                                 -Label = '<label string Here>'
                                 -Value = '<value string here>'
                         -{}
            -{}
            -{}

这是AJAX电话的要点......

function exportPatientDetailsToExcel(caseId)
{
    var caseReviewExcelTable = createTableObj();
    $.ajax({
        url: '/CasesToReview/ExportPatientDetailsToExcel',
        type: 'POST',
        contentType : 'application/json; charset=utf-8',
        dataType : 'json',
        data: JSON.stringify({ CaseReviewExcelTable: caseReviewExcelTable }),
        success: function (result) {
            console.log('got details')
        },
        dataType: 'HTML'
    });
}

function createTableObj()
{
    var header = $('#tblPatientDetails tr').first().find('b').text()
    var caseId = $('#tblPatientDetails tr').first().find('input[type="hidden"]#caseId').val()
    var rows = new Array();
    $('#tblPatientDetails tr').not(':first').each(function () {
        var items = new Array();
        $(this).children('td.detailsNameLbl').each(function () {
            var lbl = $(this).text();
            var val = $(this).next().text();
            var item = { Label: lbl, Value: val };
            items.push({ Item: item });
        });
        var row = { Items: items };
        rows.push({ Row: row });
    });
    var caseReviewExcelTable = { CaseID: caseId, Header: header, Rows: rows };
    return caseReviewExcelTable;
}
  • 编辑JasonP的评论工作,下面是我改变的内容,现在除了Item.label和Item.Value之外,所有内容都是填充的,它们仍然是空的,我仍然坚持现在。以下是我必须的其他两行

     var header = $('#tblPatientDetails tr').first().find('b').text()
        var caseId = $('#tblPatientDetails tr').first().find('input[type="hidden"]#caseId').val()
        var rows = new Array();
        $('#tblPatientDetails tr').not(':first').each(function () {
            var items = new Array();
            $(this).children('td.detailsNameLbl').each(function () {
                var lbl = $(this).text();
                var val = $(this).next().text();
                var item = { Label: lbl, Value: val };
                ***items.push(item);***
            });
            var row = { Items: items };
            ***rows.push(row);***
        });
        var caseReviewExcelTable = { CaseID: caseId, Header: header, Rows: rows };
        return caseReviewExcelTable;
    }
    

0 个答案:

没有答案