I am creating a test project where i have products, i didn't won't to use a database at the beginning so i made Mocks Data that returns a list of products, but i want a user should be able to add to the list.
This is the Model
namespace StoreTest.Models
{
public class Products
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
This is the Mock data class
using System.Collections.Generic;
using StoreTest.Models;
namespace StoreTest.Data.Mocs
{
public class ProductMocks
{
public IList<Products> ProductList
{
get
{
return
new List<Products>
{
new Products
{
Id = 1,
Name = "Some Data",
Price = 34.00
},
new Products
{
Id = 2,
Name = "More Data",
Price = 28.00
}
};
}
}
}
}
This is the part from the Controller where i want to add to the ProductList
[HttpPost]
public IActionResult NewProduct(NewProductViewModel vm)
{
if (ModelState.IsValid)
{
Products product = new Products()
{
Id = vm.Id,
Name = vm.Name,
Price = vm.Price
};
ProductMocks addProduct = new ProductMocks();
// THIS IS NOT WORKING
productMocs.ProductList.Add(product);
return View("Index", addProduct);
}
else
{
return View(vm);
}
}
I am very new to asp.net.
Idon't know if i need to change the whole ProductMocks class, or i only need to add something in the controller?
Thank you in advanced!