我想获取对象的具体信息,如果有以下内容,我该怎么做呢? 我有javascript代码
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)
并且必须从mvc controller返回值:
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;
}
但我的结果不好
答案 0 :(得分:0)
当你这样做时,控制器会收到一个名为id
的属性的对象,而不仅仅是整数。
一种方法是接受自定义类型(实际上任何具有名为id
的int属性的可序列化类型都可以。)
class IntHolder
{
public int id {get;set;}
}
public Products Editproducts([FromBody] IntHolder holder)
{
// Products Product = null;
DBEntities bd = new DBEntities();
Products SenMes = bd.Products.Find(holder.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;
}