skip to Main Content

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. 1)Initialize items

    [HttpPost]
    public trnPurchaseOrderLists ChangeQuantity(string id,decimal value)
    {
         trnPurchaseOrderLists hey = new trnPurchaseOrderLists();
         hey.Items = new mfItems();//Initialize items here
         hey.Items.ItemID = id;
         hey.Quantity = value;
         return hey;
    }
    

    2)Change itemid to itemId;

    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);//changed itemid to itemId;
                console.log(data.quantity);
            }
        });
    }
    
    Login or Signup to reply.
  2. You need to create a ViewModel class

    public class ViewModel
    {
    public int Id {get; set;}
    public decimal Value {get; set;}
    }
    

    fix action

    public IActionResult ChangeQuantity(ViewModel viewModel)
    {
      var id=viewModel.Id;
      var value=viewModel.Value
    .....
    return Ok(hey);
    }
      
    

    and fix ajax

    function change_quantity(id) {
        var value = $('#' + id).val();
        $.ajax({
            type: "POST",
            url: "/ ..controller.../ChangeQuantity",
            data: { id: id, value: value },
            success: function (data) {
                console.log(data.items.itemid);
                console.log(data.quantity);
            }
        });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search