여기 컨트롤러에 Json 객체를 전달해야합니다. 그러나 MVC JsonResult 컨트롤러 영역에 문제가 있습니다. 나는 그 물건들을 거기에서 어떻게 얻는 지 전혀 모른다. ItemQtys에는 여러 값이 있습니다.MVC 컨트롤러에 Json 데이터 전달
내 코드 :
$("#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에서 모델을 보지 못했습니다 .... –
OP가 ..... – TechGuy
으로 다시 업데이트되었지만 여전히 매개 변수가 컨트롤러 POST 동작과 일치하지 않습니다. 내가 말하고있는 것은 모델에 바인딩 된 폼을 직렬화하지 않는다는 것입니다. 그렇지 않으면 클라이언트 측에서 모델 구조를 구성하지 않아서 컨트롤러 동작에 게시됩니다. –