|
using System.Collections.ObjectModel; |
|
using System.Runtime.CompilerServices; |
|
using System.Security.Cryptography.X509Certificates; |
|
using Microsoft.AspNetCore.Mvc; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace MyApplication.Web.Controllers |
|
{ |
|
public class ProductControl : Controller |
|
{ |
|
public async Task<IActionResult> Edit() |
|
{ |
|
//ProductEditViewModel a = new ProductEditViewModel(); |
|
ProductEditViewModel a = new ProductEditViewModel(); |
|
product_edits model = new product_edits(); |
|
a.queryDate = DateTime.Now.ToShortDateString(); |
|
|
|
var Database = new MyAppDbContext(); |
|
|
|
var temp = Database.Product.Select(pro => new product_edits |
|
{ |
|
ProductId = pro.ProductId.ToString(), |
|
}) |
|
.ToList(); |
|
|
|
a.ProductEdits = new Collection<product_edits>(temp); |
|
|
|
foreach (var aProductEdit in a.ProductEdits) |
|
{ |
|
aProductEdit.brand = Database.Product |
|
.SingleOrDefault(pro2 => pro2.ProductId == Convert.ToInt16(aProductEdit.ProductId)) .BrandName; |
|
aProductEdit.Color = Database.Product.SingleOrDefault(pro2 => pro2.ProductId == Convert.ToInt16(aProductEdit.ProductId)).Color; |
|
} |
|
|
|
foreach (var aProductEdit in a.ProductEdits) { |
|
if (aProductEdit.brand == "Carrier") { |
|
aProductEdit.Sale = "This is 10% off!"; |
|
if (aProductEdit.brand == "Mitsubishi") |
|
{ |
|
aProductEdit.Sale = "This is 20% off!"; |
|
} |
|
} |
|
} |
|
|
|
a.queryDate = DateTime.Now.ToShortDateString(); |
|
return View("Edit", a); |
|
} |
|
|
|
[HttpPost("Edit")] |
|
public IActionResult Edit_Product(ProductEditViewModel Products) |
|
{ |
|
var Database = new MyAppDbContext(); |
|
var db = Database.Product |
|
.Single(x => x.ProductId.ToString() == Products.ProductEdits[0].ProductId); |
|
//Database.Entry(db).CurrentValues.SetValues(Products); |
|
//await Database.SaveChangesAsync(); |
|
// todo I could not get to work. |
|
string productsCmd = @"UPDATE Products" |
|
+ "SET Name = Quantity = {quantity}" |
|
+ string.Format("WHERE Id = {0}", Products.ProductEdits[0].ProductId); |
|
int id = Database.Database.ExecuteSql(FormattableStringFactory.Create(productsCmd)); |
|
Console.WriteLine(string.Format("Edited, {0}", Products.ProductEdits[0].ProductId)); |
|
return View(Products); |
|
} |
|
} |
|
} |
|
|
|
public class ProductEditViewModel |
|
{ |
|
public string queryDate { get; set; } |
|
public Collection<product_edits> ProductEdits { get; set; } |
|
} |
|
|
|
public class product_edits |
|
{ |
|
public string ProductId { get; set; } |
|
public string brand { get; set; } |
|
public string Color { get; set; } |
|
public string Sale { get; set; } |
|
} |
|
|
|
public class MyAppDbContext : DbContext |
|
{ |
|
public HashSet<Product> Product { get; set; } |
|
} |
|
|
|
public class Product |
|
{ |
|
public int ProductId { get; set; } |
|
public string BrandName { get; set; } |
|
public string Color { get; set; } |
|
public string Quantity { get; set; } |
|
} |