6

参数string[] orderTypeNames即将为空。

mvc 动作

public PartialViewResult EditMultipleOrderStates(
    string[] orderTypeNames,
    int[] orderIds)

javascript

$('#edit-mulitple-order-states-button').click(function () {
    ids = [];
    types = [];
    $checked = $('.order-queue-order input:checked');
    $orders = $checked.closest('.order-queue-order');
    $orders.each(function (index, elem) {
        $order = $(elem);
        ids.push($order.attr("orderId"));
        types.push($order.attr("orderType"));
    });
    data = {
        orderIds: ids,
        orderTypeNames: types
    };
    $.post('EditMultipleOrderStates', data, function (response) {
        //...
    });
});

提琴手

在此处输入图像描述

orderIds%5B%5D=97&orderIds%5B%5D=98&orderIds%5B%5D=93&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder

是引起问题的方括号吗?如何绑定到这些数组?

编辑:我同时手动构建查询字符串。

query = "";
for each...
query += "orderIds=" + $order.attr("orderId") + "&";
query += "orderTypeNames=" + $order.attr("orderType") + "&";
4

2 回答 2

3

您需要调用某种JSON.stringify数据。

 $.post('EditMultipleOrderStates', JSON.stringify(data), function (response) {
        //...
    });
于 2012-07-20T16:04:03.723 回答
1

@本杰明。这是我所做的更详细的示例,它有效

<script type="text/javascript">
    $(document).ready(function() {
        $('#btnTest').click(function() {

            var filter = {
                OrgId: 3,
                Drivers: [{ Name: "SGT-200" }, { Name: "SGT-100"}],
                DrivenUnits: [{ Name: "Generator" }],
                Regions: [{ Name: "REU" }],
                SearchString : "Test search string"
            };

            $.ajax(
                {
                    url: "/api/Dashboard/PostFilter",
                    type: "POST",
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(filter),
                    success: function(result) {
                        alert(result);
                    },
                    error: function(error) {

                        alert("There was an error posting the data to the server: " + error.responseText);

                    }
                });
        });
    });
</script>

希望这可以帮助。

编辑:这里是匹配的 C# 对象

public class Filter
{
    public int OrgId { get; set; }
    public List<FilterData> Drivers { get; set; }
    public List<FilterData> DrivenUnits { get; set; }
    public List<FilterData> Regions { get; set; }
    public List<FilterData> Operations { get; set; }
    public List<FilterData> Connections { get; set; }
    public string SearchString { get; set; }
}

public class FilterData
{
    public string Type { get; set; }
    public string Name { get; set; }
    public int Count { get; set; }
}

您所有的类型名称都需要匹配。下面是接收呼叫的代码

public IQueryable<DashboardData> Post(Filter filter) {
        using(_unitOfWork)
        {
            _dashboardRepository.UsingUnitOfWork(_unitOfWork);

            return FilterDashboardData(_dashboardRepository.GetDashboardData(filter.OrgId), filter);
        }
    }
于 2012-07-23T08:50:51.307 回答