MVC3和JSON.stringify()ModelBinding返回null模型

shu*_*iar 7 c# asp.net jquery json asp.net-mvc-3

我试图让模型绑定MVC3和JSON工作,但我没有运气...无论我做什么,我似乎null在服务器上得到一个模型.

方法签名:

public ActionResult FilterReports(DealSummaryComparisonViewModel model)
Run Code Online (Sandbox Code Playgroud)

Javascript 更新:

<script type="text/javascript" language="javascript">

    $(document).ready(function () {
        $('#filter-reports').click(filterReports);
    });

    function filterReports() {
        var filters = {
            SelectedRtoId: $('#SelectedRtoId').val(),
            SelectedPricingPointId: $('#SelectedPricingPointId').val(),
            SelectedLoadTypeId: $('#SelectedLoadTypeId').val(),
            SelectedBlockId: $('#SelectedBlockId').val(),
            SelectedRevisionStatusId: $('#SelectedRevisionStatusId').val()
        }
        var dealSummaries = { SelectedItemIds: $('#SelectedItemIds').val() }
        var model = { ReportingFilters: filters, DealSummaries: dealSummaries }

        $('#selected-items select option').attr("selected", "selected");
        $.ajax({
            url: '@Url.Action("FilterReports")',
            data: model,
            contentType: 'application/json',
            dataType: 'json',
            success: function (data) {
                alert(data);
            }
        }); 
    }
</script>
Run Code Online (Sandbox Code Playgroud)

楷模:

public class DealSummaryComparisonViewModel
{
    public ReportingFiltersViewModel ReportingFilters { get; set; }
    public LadderListViewModel DealSummaries { get; set; }
}

public class LadderListViewModel
{
    public MultiSelectList AvailableItems { get; set; }

    public int[] SelectedItemIds { get; set; }
    public MultiSelectList SelectedItems { get; set; }
}

public class ReportingFiltersViewModel
{
    public int? SelectedRtoId { get; set; }
    public ICollection<Rto> Rtos { get; set; }

    public int? SelectedPricingPointId { get; set; }
    public ICollection<PricingPoint> PricingPoints { get; set; }

    public int? SelectedLoadTypeId { get; set; }
    public ICollection<LoadType> LoadTypes { get; set; }

    public int? SelectedBlockId { get; set; }
    public ICollection<Block> Blocks { get; set; }

    public int? SelectedRevisionStatusId { get; set; }
    public ICollection<RevisionStatus> RevisionStatuses { get; set; }

    public bool? DealStatus { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

该模型在客户端看起来很好:

{"ReportingFilters":{
    "SelectedRtoId":"5",
    "SelectedPricingPointId":"20",
    "SelectedLoadTypeId":"55",
    "SelectedBlockId":"21",
    "SelectedRevisionStatusId":"11" 
},"DealSummaries":{
    "SelectedItemIds":["21","22","23","24","25"] 
}}
Run Code Online (Sandbox Code Playgroud)

那么为什么我什么都得不到控制器?过去两天这给了我麻烦所以请帮忙!谢谢!!

更新 我已将我的javascript部分更新为我目前正在使用的部分.此部分现在使用ReportingFilers和DealSummaries对象将模型返回给控制器,但其中的所有值都为null.

是否可能与值为字符串有关?如果是这样,我该如何解决这个问题呢?

Ric*_*ton 4

将 $.getJSON 行更改为:

$.ajax({ 
   url: '@Url.Action("FilterReports")',
   data: JSON.stringify(viewModel),
   contentType: 'application/json',
   dataType: 'json',
   success: function (data) { alert(data); }
});
Run Code Online (Sandbox Code Playgroud)

这样,MVC 就知道它正在接收 JSON 并将其正确绑定到您的模型。