如何在mvc4中使用json传递多个参数?

时间:2016-03-04 05:21:52

标签: c# jquery json ajax asp.net-mvc-4

您好我想使用json将多个参数传递给控制器​​。我在View FromDate,ToDate,CustomerName,Count中有四个字段。如果我选择FromDate。 ToDate,CustomerName并单击它已计算的ok按钮,并按照FromDate,ToDate,CustomerName中选择的值在Count Textbox中显示值。

我的控制器

[HttpGet]
public ActionResult NewExistingCustomer( CustomerViewModel cvm)
{
    ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName");
    return View();
}

public JsonResult GetCustomers()
{
    return Json(db.Customers.ToList(), JsonRequestBehavior.AllowGet);
}

public JsonResult GetSalesOrderCountByCustomer(Guid customerID, DateTime   fromdate, DateTime Todate)
{
    var salescount = (from sc in db.SalesOrders where sc.CustomerID == customerID && sc.CreatedDate == fromdate && sc.CreatedDate == Todate select     sc.SalesOrderID).Count();
    return Json(salescount, JsonRequestBehavior.AllowGet);
}

这里我从SalesOrderTable计算SalesOrderCount取决于从View获取FromDate ToDate CustomerName的值,然后我在Count Textbox中显示值。 但它没有用。我不知道我错了。请任何人再次交叉检查我的代码,它显示错误

Error

我的观点

<div class="col-sm-4">
    <div class="form-group">
        @Html.Label("FromDate", new { @class = "control-label" })
        @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text"})
    </div>
</div>

<div class="col-sm-4">
    <div class="form-group">
        @Html.LabelFor(model => model.ToDate)
         @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
    </div>
</div>

<div class="col-sm-4">
    <div class="form-group">
        @Html.LabelFor(model => model.CustomerName)
        @Html.DropDownList("CustomerID","Select")
        @Html.ValidationMessageFor(model => model.CustomerName)
    </div>
</div>

<div class="col-sm-4">
    <div class="form-group">
        @Html.LabelFor(model => model.count)
        @Html.TextBoxFor(model => model.count, new { @class = "form-control", type = "text"})
        @Html.ValidationMessageFor(model => model.count)
    </div>
</div>

我的Jquery

function ok() {
    var customerID = $("#CustomerID").val();
    var fromdate = $("#FromDate").val();
    var Todate = $("ToDate").val();
    var ordercount = { "CustomerID": customerID, "FromDate": fromdate, "ToDate": Todate };
    $.ajax(
        '@Url.Action("GetSalesOrderCountByCustomer", "Report")', {
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: JSON.stringify(ordercount),
        error: function (ex) {
            alert('Failed to retrieve Email.' + ex);
        },
        beforeSend: function () {
        },
        success: function (data) {
            $("#count").val(data);
        }
    });
}

提前谢谢

1 个答案:

答案 0 :(得分:0)

这是一种更简化的方法。

function ok() {
    var customerID = $("#CustomerID").val();
    var fromdate = $("#FromDate").val();
    var Todate = $("ToDate").val();
    var ordercount = { "CustomerID": customerID, "FromDate": fromdate, "ToDate": Todate };
/*$.ajax(
        '@Url.Action("GetSalesOrderCountByCustomer", "Report")', {
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: JSON.stringify(ordercount),
        error: function (ex) {
            alert('Failed to retrieve Email.' + ex);
        },
        beforeSend: function () {
        },
        success: function (data) {
            $("#count").val(data);
        }
    });*/

    $.get('/Report/GetSalesOrderCountByCustomer', ordercount, function(response)
    {
        $('#count').val(respose);
    });
}