我正在使用asp.net mvc创建一个简单的销售点系统。我们有销售和销售产品以及产品表。当完成的销售必须将数据保存到相关表中时,同时需要减少产品表的产品数量。但是只有一个转换有效,销售表才有效。其他人不起作用(销售产品和产品)。我在下面所做的一切。
[HttpPost]
public ActionResult SaveNew(string data)
{
ajaxModel model = JsonConvert.DeserializeObject<ajaxModel>(data);
bool status = false;
try
{
using (aspposEntities1 db = new aspposEntities1())
{
var sale = new sale
{
subtotal = model.total,
pay = model.pay,
balance = model.balance
};
db.sales.Add(sale);
db.SaveChanges();
model.data.ForEach( m =>
{
var db_product = db.products.First(e => e.id.ToString() == m.barcode_id.ToString());
db_product.qty = db_product.qty - m.qty;
db.sales_product.Add(new sales_product
{
sales_id = sale.id.ToString(),
barcode_id = m.barcode_id.ToString(),
price = m.pro_price,
qty = m.qty,
total = m.total_cost
});
});
db.SaveChanges();
status = true;
}
return new JsonResult { Data = new { status, message = "Entry saved successfully" } };
}
catch (Exception e)
{
return new JsonResult { Data = new { status, message = "There was an error saving the Entry" } };
}
}
tableview.cs
public class tableView
{
public int barcode_id { get; set; }
public string pname { get; set; }
public int pro_price { get; set; }
public int qty { get; set; }
public int total_cost { get; set; }
}
public class ajaxModel
{
public List<tableView> data { get; set; }
public int total { get; set; }
public int pay { get; set; }
public int balance { get; set; }
public string orderid { get; set; }
}
**product table**
public partial class product
{
public int id { get; set; }
public string proname { get; set; }
public Nullable<int> cat_id { get; set; }
public Nullable<int> brand_id { get; set; }
public Nullable<int> qty { get; set; }
public Nullable<int> price { get; set; }
}
sales table
public partial class sale
{
public int id { get; set; }
public Nullable<int> subtotal { get; set; }
public Nullable<int> pay { get; set; }
public Nullable<int> balance { get; set; }
}
sales product table
public partial class sales_product
{
public int id { get; set; }
public string sales_id { get; set; }
public string barcode_id { get; set; }
public Nullable<int> price { get; set; }
public Nullable<int> qty { get; set; }
public Nullable<int> total { get; set; }
}**