【发布时间】: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 inReportController` -
等待我将显示我更新的代码
-
$.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