如何在mvc4中将多个参数传递给ajax并从ajax传递给控制器​​?

时间:2016-03-03 09:32:06

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

我的第一个问题

您好我在我的视图FromDate,ToDate,CustomerName,Count中有4个字段。 我想将FromDate,ToDate,CustomerName的值传递给单个Action,并且必须在Count Textbox中获取值。帮我解决这个问题。

我的第二个问题

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

以上代码现在得到客户的SalesOrder总数我需要的是我想得到FromDate(我在视图中选择)和ToDate(我选择)之间的客户(我在下拉列表中选择)的销售订单数量在视图中)。我如何根据我的要求更改此查询。

我的观点

  <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

   <script src="~/Scripts/jquery-ui-1.8.24.js"></script>
   <script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
   <script src="~/Scripts/jquery-1.7.1.min.js"></script>
   <script src="~/Scripts/jquery-ui.js"></script>
   <script src="~/Scripts/jquery-ui-1.11.0.js"></script>
   <script type ="text/javascript">

$(function () {
    $.ajax(

        //url: "/VisitorsForm/GetCustomers",
        '@Url.Action("GetCustomers","Report")', {
            type: "GET",
            datatype: "Json",
            success: function (data) {
                $.each(data, function (index, value) {
                    $('#CustomerID').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>');
                });

            }
        });
});

  $("#CustomerID").change(function () {
    //$('#ContactID').empty();
    alert("hhh");
    var req = {
        CustomerID: $("#CustomerID").val(),
        FromDate: $("#FromDate").val(),
        ToDate: $("ToDate").val()
    }

    $.ajax(
        '@Url.Action("GetSalesOrderCountByCustomer", "Report")', {
            type: "GET",
            dataType: "json",
            async: false,
            data: JSON.stringify(req),
            error: function (ex) {
                alert('Failed to retrieve Email.' + ex);
            },
            beforeSend: function () {
            },
            success: function (data) {
                $("#count").val(data);
            }
        });

});

</script>

我的控制器

    [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 select sc.SalesOrderID).Count();
        return Json(salescount, JsonRequestBehavior.AllowGet);
    }
    }}

0 个答案:

没有答案