Hi
i Create a Project with .core and asp.net Core 2.0.0 === SportsStore in pdf Pro Asp.net Core MVC Adam Freeman
i Create a Custome TagHelper :
namespace SportsStore.Infrastructure { [HtmlTargetElement("div",Attributes = "page-model")] public class PageLinkTagHelper:TagHelper { private IUrlHelperFactory urlHelperFactory; public PageLinkTagHelper(IUrlHelperFactory helperFactory) { urlHelperFactory = helperFactory; } [ViewContext] [HtmlAttributeNotBound] public ViewContext ViewContext { get; set; } public PagingInfo PageModel { get; set; } public string PageAction { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext); TagBuilder result = new TagBuilder("div"); for (int i = 1; i <= PageModel.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.Attributes["href"] = urlHelper.Action(PageAction, new { page = i }); tag.InnerHtml.Append(i.ToString()); result.InnerHtml.AppendHtml(tag); } output.Content.AppendHtml(result.InnerHtml); } } }
and i Change Route System :
app.UseMvc(routes => { routes.MapRoute( name: "pagination", template: "Products/Page{page}", defaults: new { Controller = "Product", action = "List" }); routes.MapRoute( name: "default", template: "{controller=Product}/{action=List}/{id?}"); });
next when i Run my Project and clicked on Number From Pagination i Get Error :
An unhandled exception occurred while processing the request. InvalidOperationException: The view 'List' was not found. The following locations were searched: /Views/Shared/List.cshtml
code in Controller :
public class ProductController : Controller { private IProductRepository repository; public int PageSize = 4; public ProductController(IProductRepository repo) { repository = repo; } // GET: /<controller>/ public ViewResult List(int page = 1) => View(new ProductsListViewModel {Products= repository.Products .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo=new PagingInfo { CurrentPage=page,ItemsPerPage=PageSize,TotalItems=repository.Products.Count() } }); }
code in List.cshtml :
@model ProductsListViewModel @foreach (var p in Model.Products) { <div><h3>@p.Name</h3> @p.Description<h4>@p.Price.ToString("c")</h4></div> }<div page-model="@Model.PagingInfo" page-action="List"></div>
i have list.cshtml in Folder Product But Why i Get This Error ?? How Can I solve It .....