1

我会尝试得到这样的结果:

function GetPrice() {
        var url = '@Url.Action("GetAdditionalPrice")';
        $.post(url, { additionalsId: $("input[name=selectedAdditionals]").val }),
        function(data) {
            $("#TotalAmount").val(data);
        }; //View
    }

public ActionResult GetAdditionalPrice(string[] additionalsId)
    {
        decimal price = 0;
        foreach (var id in additionalsId)
        {
            var aid = int.Parse(id);
            price += db.ContractAdditionals.Where(ca => ca.ContractAdditionalID == aid).Select(ca => ca.Price).Single();
        }

        return Json(price, JsonRequestBehavior.AllowGet);
    }//Controller

为什么string[] additionalsId参数总是未定义?如何将参数发布到控制器并将结果设置为#TotalAmount

4

3 回答 3

0

你错过了()你的.val声明。那应该在控制台中引发 JS 错误。

要传回一个数组,您还需要使用您选择的值构建一个

尝试这个:

function GetPrice() {
    var url = '@Url.Action("GetAdditionalPrice")';
    var items = new Array();
    $.each($("input[name=selectedAdditionals]:checked"), function () {
        items.push($(this).val());
    });

    $.post(url, { additionalsId: items }),
    function(data) {
        $("#TotalAmount").val(data);
    }; //View
}
于 2013-04-25T14:30:53.743 回答
0

它解决了

function GetPrice() {
        var url = '@Url.Action("GetAdditionalPrice")';
        var items = new Array();
        $("input[name=selectedAdditionals]:checked").each(function () {
            items.push($(this).val());
        });
        var postData = { additionalsId: items };
        $.ajax({
            type: "POST",
            url: url,
            data: postData,
            success: function (data) {
                $("#TotalAmount").val(data.result);
            },
            dataType: "json",
            traditional: true
        });
    }

感谢帮助 :)

于 2013-04-25T15:49:55.077 回答
0

尝试这个

function GetPrice() {
    var url = '@Url.Action("GetAdditionalPrice")';
    $.post(url, { additionalsId: $("input[name=selectedAdditionals]").val() }),
    function(data) {
        $("#TotalAmount").val(data.result);
    }; //View
}

public ActionResult GetAdditionalPrice(string[] additionalsId)
{
    decimal price = 0;
    foreach (var id in additionalsId)
    {
        var aid = int.Parse(id);
        price += db.ContractAdditionals.Where(ca => ca.ContractAdditionalID == aid).Select(ca => ca.Price).Single();
    }

    return Json(new {result=price}, JsonRequestBehavior.AllowGet);
}//Controller
于 2013-04-25T14:45:58.530 回答