【问题标题】:How to pass multiple parameter using json in mvc4?如何在 mvc4 中使用 json 传递多个参数?
【发布时间】:2016-03-04 05:21:52
【问题描述】:

您好,我想使用 json 将多个参数传递给控制器​​。我的视图 FromDate、ToDate、CustomerName、Count 中有四个字段。如果我选择 FromDate。 ToDate、CustomerName 并单击它已计算的确定按钮,并根据在 FromDate、ToDate、CustomerName 中选择的值在计数文本框中显示值。

我的控制器

[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 文本框中显示该值。 但它不起作用。我不知道我在哪里做错了。请任何人再次交叉检查我的代码,它显示错误

我的观点

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

提前谢谢

【问题讨论】:

  • '@Url.Action("GetSalesOrderCountByCustomer", "Report")', 必须是 url: @Url.Action("GetSalesOrderCountByCustomer", "Report")',(以及之前的 {
  • 没有斯蒂芬它不工作
  • 什么不起作用?必须是$.ajax({ url: @Url.Action("GetSalesOrderCountByCustomer", "Report")', type: "GET", ...}); and it will hit your method assuming that method is in ReportController`
  • 等待我将显示我更新的代码
  • $.ajax({ url:'@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 () { },成功:函数(数据){$("#count").val(data);} });

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


【解决方案1】:

这是一种更简化的方法。

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

【讨论】:

  • no kosala 它显示相同的页面错误,但您的代码是正确的,但我不知道为什么不工作
  • 我想我知道为什么。使用string CustomerID 而不是Guid customerID
  • 参数字典包含方法“System.Web.Mvc.JsonResult GetSalesOrderCountByCustomer(System.Guid, System.DateTime, System.DateTime)”在“Sample_Customer.Controllers.ReportController”中。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:使用/Report/GetSalesOrderCountByCustomer直接点击的参数我得到这个错误 -
  • Guid类型参数转换成字符串类型了吗?
  • 是的,但我在这一行出现错误,尤其是在 sc.CustomerID == customerID 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();返回 Json(salescount, JsonRequestBehavior.AllowGet); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 2019-09-04
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多