This is my first time asking here. I’m just wondering if there’s a way to send a model data inside another model from ajax to controller.
Here’s my model:
public class mfItems
{
[Key]
[Display(Name = "Item ID")]
public string ItemID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Model Description")]
public string ItemModelDescription { get; set; }
[Display(Name = "Unit Price")]
[Required(ErrorMessage = "Required")]
public decimal ItemUnitPrice { get; set; }
}
And my other model:
public class trnPurchaseOrderLists
{
public mfItems Items { get; set; }
public decimal Quantity { get; set; }
}
Here’s my ajax and in this ajax I tried to log the data.items.itemid
but this isn’t sending an id but when I remove the console.log(data.items.itemid)
and to the controller hey.Items.ItemID = id
another data which is quantity is working very well.
function change_quantity(id) {
var value = $('#' + id).val();
$.ajax({
type: "POST",
url: "@Url.Action("ChangeQuantity")",
data: { id: id, value: value },
dataType: "json",
success: function (data) {
console.log(data.items.itemid);
console.log(data.quantity);
}
});
}
Lastly here’s my controller
[HttpPost]
public trnPurchaseOrderLists ChangeQuantity(string id,decimal value)
{
trnPurchaseOrderLists hey = new trnPurchaseOrderLists();
hey.Items.ItemID = id;
hey.Quantity = value;
return hey;
}
Where am I wrong? please help thanks. If you find my English is very confusing I apologize. I’m really not good at English. Thank you so much
2
Answers
1)Initialize items
2)Change itemid to itemId;
You need to create a ViewModel class
fix action
and fix ajax