MVC3&安培; JSON.stringify()ModelBinding返回null模型 [英] MVC3 & JSON.stringify() ModelBinding returns null model

查看:114
本文介绍了MVC3&安培; JSON.stringify()ModelBinding返回null模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让模型MVC3和JSON工作结合,但我有没有运气......不管我做什么,我似乎得到一个模型在服务器上。

I am trying to get model binding with MVC3 and JSON working but I have had no luck... No matter what I do I seem to be getting a null model on the server.

方法签名:

public ActionResult FilterReports(DealSummaryComparisonViewModel model)

Java脚本的更新时间:

<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>

模型:

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; }
}

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

The model looks fine on the client side:

{"ReportingFilters":{
    "SelectedRtoId":"5",
    "SelectedPricingPointId":"20",
    "SelectedLoadTypeId":"55",
    "SelectedBlockId":"21",
    "SelectedRevisionStatusId":"11" 
},"DealSummaries":{
    "SelectedItemIds":["21","22","23","24","25"] 
}}

那么,为什么我得到什么控制器背面的?这已经给我找麻烦了,这两天,请帮助!谢谢!

So why am I getting nothing back on the controller? This has been giving me trouble for the past two days so please help! Thanks!!

更新
我已经更新了我的JavaScript部分是什么我现在使用到。本节现在返回模型与ReportingFilers和DealSummaries对象控制器,但所有值之内是空的。

UPDATE I've updated my javascript section to what I am currently using. This section now returns the model to the controller with the ReportingFilers and DealSummaries objects, but all values within are null.

是否可能有事情做的价值观是字符串?如果是这样,我怎么能解决这个问题?

Does it possibly have something to do with the values being strings? If so, how can I fix this?

推荐答案

您$ .getJSON行更改为:

Change your $.getJSON line to:

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

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

This way MVC knows that it is receiving JSON and will bind it to your model correctly.

这篇关于MVC3&安培; JSON.stringify()ModelBinding返回null模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆