Ошибка возникает когда нажимаю на завершения заказа
P.S я добавил EntityFrameworkCore и создал все нужные таблицы
namespace WebApplication29.Data.Repository
{
public class OrdersRepository : IAllOrders
{
private readonly AppDBContent appDBContent;
private readonly ShopCart shopCart;
public OrdersRepository(AppDBContent appDBContent, ShopCart shopCart)
{
this.appDBContent = appDBContent;
this.shopCart = shopCart;
}
public void createOrder(Order order)
{
order.orderTime = DateTime.Now;
appDBContent.Order.Add(order);
var items = shopCart.listShopItems;
foreach(var el in items)
{
var orderDetail = new OrderDetail()
{
CarId = el.car.id,
orderID = order.id,
price = el.car.price
};
appDBContent.OrderDetail.Add(orderDetail);
}
appDBContent.SaveChanges();
}
}
}
public class OrderController : Controller
{
private readonly IAllOrders allOrders;
private readonly ShopCart shopCart;
public OrderController(IAllOrders allOrders, ShopCart shopCart)
{
this.allOrders = allOrders;
this.shopCart = shopCart;
}
public IActionResult Checkout()
{
return View();
}
[HttpPost]
public IActionResult Checkout(Order order)
{
shopCart.listShopItems = shopCart.getShopItems();
if(shopCart.listShopItems.Count == 0)
{
ModelState.AddModelError("", "У вас должны быть товары!");
}
if (ModelState.IsValid)
{
allOrders.createOrder(order);
return RedirectToAction("Complete");
}
return View(order);
}
public IActionResult Complete()
{
ViewBag.Message = "Заказ успешно обработан";
return View();
}
}
@model Car
<div class="col-lg-4">
<img class="img-thumbnail pic" src="@Model.img" alt="@Model.name" />
<h2>Модель: @Model.name</h2>
<p>@Model.shortDesc</p>
<p>Цена: @Model.price.ToString("c")</p>
<p><a class="btn btn-warning" asp-controller="ShopCart" asp-action="addToCart" asp-route-id="@Model.id">Добавить в корзину</a></p>
</div>
public class Startup
{
private IConfigurationRoot _confSting;
public Startup(IWebHostEnvironment hostEnv)
{
_confSting = new ConfigurationBuilder().SetBasePath(hostEnv.ContentRootPath).AddJsonFile("dbsettings.json").Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDBContent>(options => options.UseSqlServer(_confSting.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
services.AddTransient<IAllCars, CarRepository>();
services.AddTransient<ICarsCategory, CategoryRepository>();
services.AddTransient<IAllOrders, OrdersRepository>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped(sp => ShopCart.GetCart(sp));
services.AddSession();
services.AddMvc();
services.AddMemoryCache();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "categoryFilter",
pattern: "Car/{action}/{category?}", defaults: new { Controller="Car", action="List"});
});
app.UseStaticFiles();
using (var scope = app.ApplicationServices.CreateScope())
{
AppDBContent content = scope.ServiceProvider.GetRequiredService<AppDBContent>();
DBObjects.Initial(content);
}
}
}