【发布时间】:2014-07-14 11:15:43
【问题描述】:
这里我需要将 Json 对象传递给控制器。但是我在 MVC JsonResult 控制器区域有问题。我不知道如何将这些对象放在那里。在 ItemQtys 中有多个值。
我的代码:
$("#btnSubmit").click(function (e) {
var itemQtys = [];
var currentQty;
$('input.input-qty').each(function (itm) {
currentQty = $(this).val();
if (!isNaN(currentQty) && currentQty != '') {
currentQty = parseInt(currentQty);
itemQtys.push({
ItemId: $(this).attr('data-id'),
Qty: currentQty
});
}
});
var data = {
UserId: $("#CustomerName option:selected").val(),
DeliveryInstructions: $('#DeliveryInstructions').val(),
ItemQtys: itemQtys
};
$.ajax({
type: "POST",
url: "/Orders/SubmitCustomerOrder",
contentType: "application/; charset=utf-8",
dataType: "json",
data: data,
async: false,
success: function (msg) {
if (msg.length > 0) {
$.each(function (response) {
//$("#Result").text(response.text);
});
}
}
});
});
MVC 控制器:(我想在这里获取值)
public JsonResult SubmitCustomerOrder(OrdersModels model, int? id)
{
return Json(new { });
}
我的模特:
public class OrdersModels : DbContext
{
public class CustomerOrders
{
public int CustomerOrderId { get; set; }
//public int CustomerId { get; set; }
public DateTime OrderDate { get; set; }
public DateTime DeliveryDate { get; set; }
public string DeliveryInstructions { get; set; }
public int Status { get; set; }
public int Customer_Supplier_CustomerSupplierId { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid ModifiedBy { get; set; }
public bool IsActive { get; set; }
public List<OrderItems> OrderItemList { get; set; }
}
public class OrderItems
{
public int OrderItemsId { get; set; }
public int Products_ProductId { get; set; }
public int CustomerOrders_CustomerOrderId { get; set; }
public int Quantity { get; set; }
public DateTime ModifiedDate { get; set; }
public Guid ModifiedBy { get; set; }
public bool IsActive { get; set; }
}
}
【问题讨论】:
-
您的参数与您传递的数据不匹配。我在您发布的 JSON 中没有看到您的模型......
-
再次,参数仍然与您的控制器 POST 操作不匹配。我的意思是您没有序列化绑定到模型的表单,或者您没有在客户端构建模型结构然后发布到控制器操作。
标签: c# asp.net-mvc json asp.net-mvc-4