I send an id from the page to the server, the answer comes - this is a model object found by id, how can I get the item (Price) I need from it? Script:

var data = { Id: null, Name_Product: null, Price: null, Description: null } $.ajax({ url: '/editpr', type: 'POST', data: JSON.stringify({ id: tested }), contentType: "application/json; charset=utf-8", //dataType: 'JSON', success: function (data) { var Price = data.Price var newinput = document.createElement('input') newinput.className = 'save_inp' newinput.id = 'id_save_inp' newinput.value = Price $('.price').html(newinput) 

Controller Code:

  public Products Editproducts([FromBody] int id) { // Products Product = null; DBEntities bd = new DBEntities(); Products SenMes = bd.Products.Find(id); if (SenMes != null) { Products product = new Products { Id = SenMes.Id, Name_Product = SenMes.Name_Product, Description = SenMes.Description, Price = SenMes.Price, MesAndProduct = SenMes.MesAndProduct }; // Product = SenMes; return product; } return null; } 

And that's what I get in the end ... enter image description here

How do I get rid of undefined and get a price that will match this id?

  • To begin with, decide whether the product id is correctly received and processed on the server. After that, make sure that the method returns the product, not null . And then make sure that it is correct (so that the client can parse it) is sent to the client. - Regent
  • Everything is going smoothly, I just need to find out how to get an element out of an object - Mike Stylinsky
  • If everything went smoothly, then there would be no problem. Once the data.Price does not get the value you need, it means that jQuery is not able to parse the response from the server. At least give an example of the data returned by the server. - Regent
  • Here are the values ​​that are returned prntscr.com/dmsxwr - SenMes prntscr.com/dmsy1m - Price - Mike Stylinsky
  • one
    the controller should return the heirs of ActionResult in this case, we rather need JsonResult , what is Products for you? - teran

1 answer 1

Controller:

 [HttpPost] public JsonResult Editproducts(int id) { Products SenMes = null; using (DBEntities bd = new DBEntities()) Products SenMes = bd.Products.Find(id); return Json(SenMes); } 

If you need to return not all fields SenMes, but only specific:

 return Json(new { Id=SenMes.Id, Price=SenMes.Price });