D
D
Dmitry Makarov2018-10-09 15:48:00
ASP.NET
Dmitry Makarov, 2018-10-09 15:48:00

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:

Product\List.cshtml
@model ProductsListViewModel

@foreach (var p in Model.Products)
{
    @Html.Partial("_ProductSummary", p)
}

Shared\_Layout.cshtml
<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Магазин</title>
    </head>
    <body>
           @RenderBody()
    </body>
</html>

Shared\_ProductSummary.cshtml
@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>

_ViewStart.cshtml
@{
    Layout = "_Layout";
}

_ViewImports.cshtml
@using Store.Models
@using Store.Models.ViewModels
@using Store.Infrastructure
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper Store.Infrastructure.*, Store

Startup.cs
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);
        }
    }
}


By default, we display Product\List, which is formed from partial views (@Html.Partial) _ProductSummary. _ProdictSummary contains various product data and a form with an "Add" button. Clicking on the "Add" button should generate a POST request, which should eventually run the AddToCart action method of the Cart controller, which will add the product to the shopping list and display the cart view.
In fact, the "Add" button does not work. I saw that in the HTML Product \ List in the form TagHelpers are not processed. Those. where instead of asp-action="AddToCart" should appear action=/Cart/AddToCart still remain asp-action="AddToCart". And other asp-xxx (asp-controller and asp-for) don't "unroll" either. At least I understood
Why don't TagHelpers work? How to fix?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question