Sneha K

Sneha K

  • 1.2k
  • 527
  • 196.8k

How to pass multiple parameter using json in mvc4?

Mar 3 2016 11:27 PM
Hi i want to pass Multiple parameter using json . I have four fields in my View FromDate, ToDate, CustomerName, Count. if i select the FromDate. ToDate, CustomerName and click the ok button it have calculate and display the value in Count Textbox as per value selected in FromDate, ToDate ,CustomerName
My Controller
[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);
}
Here i calculate the SalesOrderCount from SalesOrderTable depend upon getting values of FromDate ToDate CustomerName from View and then i display the value in Count Textbox.
But it is not working. I donno where i did mistake . Please any one cross check my code again , it showing error
Error
 
 
 
 
 
My View
<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>
My 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);
}
});
}
Advance Thanks..

Answers (1)