Answer the question
In order to leave comments, you need to log in
Why don't TagHelpers work?
I am learning ASP.NET CORE MVC.
The .net core version is 2.1.402.
Case study: online store.
When you click on the "Add" button, the product should be added to the cart and the cart page should be displayed.
The project has a "Views" folder, it has:
@model ProductsListViewModel
@foreach (var p in Model.Products)
{
@Html.Partial("_ProductSummary", p)
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Магазин</title>
</head>
<body>
@RenderBody()
</body>
</html>
@model Product
<div>
<div>
<div>@Model.Name</div>
<div>@Model.Price.ToString("c", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))<div>
<div>@Model.Description</div>
<div>
<form id="@Model.ProductID" asp-action="AddToCart" asp-controller="Cart" method="post">
<input type="hidden" asp-for="ProductID" />
<input type="hidden" name="returnUrl" value="@ViewContext.HttpContext.Request.PathAndQuery()" />
<button type="submit">Добавить</button>
</form>
</div>
@{
Layout = "_Layout";
}
@using Store.Models
@using Store.Models.ViewModels
@using Store.Infrastructure
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper Store.Infrastructure.*, Store
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Store.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
namespace Store
{
public class Startup
{
string _ContentRootPath;
IConfigurationRoot Configuration;
public Startup(IHostingEnvironment env)
{
_ContentRootPath = env.ContentRootPath;
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration["Data:StoreProducts:ConnectionString"]));
services.AddTransient<IProductRepository, EFProductRepository>();
services.AddScoped<Cart>(sp => SessionCart.GetCart(sp));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc();
services.AddMemoryCache();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes=>
{
routes.MapRoute(
name:null,
template:"{category}/Page{page:int}",
defaults:new {controller="Product", action="List"}
);
routes.MapRoute(
name:null,
template:"Page{page:int}",
defaults:new {controller="Product", action="List", page = 1}
);
routes.MapRoute(
name:null,
template:"{category}",
defaults:new {controller="Product", action="List", page = 1}
);
routes.MapRoute(
name:null,
template: "",
defaults:new {controller="Product", action="List", page = 1}
);
routes.MapRoute(
name: null,
template: "{controller}/{action}/{id?}"
);
});
SeedData.EnsurePopulated(app);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question