【问题标题】:ViewComponent Navigation Buttons Don't WorkViewComponent 导航按钮不起作用
【发布时间】:2020-11-01 09:16:37
【问题描述】:

我正在使用 .NET Core MVC。我尝试创建的导航链接似乎无法正常工作。我有以下型号:

public class Model
    {
        public long ModelID { get; set; }
        public string Name { get; set; }
        public string SolverType { get; set; } 
        public string ProgramType { get; set; } 

    }

传递的这个 ViewModel 是:

public class ModelsListViewModel
    {
        public IEnumerable<Model> Models { get; set; }
        public PagingInfo PagingInfo { get; set; }
        public string CurrentProgramType { get; set; }
    }

视图是:

@model IEnumerable<string>

    <a class="btn btn-block btn-outline-secondary"asp-action="Index"
       asp-controller="Home" asp-route-type="">
        Home
    </a>
    //THESE DON'T LINK APPROPRIATELY TO THE CONTROLLER
    @foreach (string type in Model)
    {
        <a class="btn btn-block btn-outline-secondary"
           asp-action="Index" asp-controller="Home"
           asp-route-type="@type"
           asp-route-modelPage="1">
            @type
        </a>
    }

Home 控制器的动作是:

 public ViewResult Index(string type, int modelPage = 1)
            => View(new ModelsListViewModel
            {
                Models = repository.Models
                    .Where(m => type == null || m.ProgramType == type)
                    .OrderBy(m => m.ModelID)
                    .Skip((modelPage - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = modelPage,
                    ItemsPerPage = PageSize,
                    TotalItems = type == null ?
                        repository.Models.Count() :
                        repository.Models.Where(e =>
                            e.ProgramType == type).Count()
                },
                CurrentProgramType = type
            });

当页面呈现视图组件按钮时显示,但链接不起作用或悬停时显示。他们似乎没有在控制器中调用任何操作。

编辑:这是我的Startup.cs 文件中定义的路由:

app.UseEndpoints(endpoints => {
                endpoints.MapControllerRoute("typepage",
                    "{type}/Page{modelPage:int}",
                    new { Controller = "Home", action = "Index" });

                endpoints.MapControllerRoute("page", "Page{modelPage:int}",
                    new { Controller = "Home", action = "Index", modelPage = 1 });

                endpoints.MapControllerRoute("type", "{type}",
                    new { Controller = "Home", action = "Index", modelPage = 1 });

                endpoints.MapControllerRoute("pagination",
                    "Models/Page{modelPage}",
                    new { Controller = "Home", action = "Index", modelPage = 1 });
                endpoints.MapDefaultControllerRoute();
            });

EDIT2:这是呈现按钮的 ViewComponent:

public IViewComponentResult Invoke()
        {
            return View(repository.Models
                .Select(x => x.ProgramType)
                .Distinct()
                .OrderBy(x => x)); ;
        }

编辑3: 这是 PageingInfo 类来解决评论左:

 public class PagingInfo
    {
        public int TotalItems { get; set; }
        public int ItemsPerPage { get; set; } 
        public int CurrentPage { get; set; }

        public int TotalPages => (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);
    }

【问题讨论】:

  • 这些路由是在Startup.cs文件中定义的路由表吗?这个控制器的动作可以直接路由吗?用于路由的 Asp 标记助手正在使用路由表从您拥有的和平中生成 URL。默认路由只定义了一个可选参数。尝试使用一个 int 参数创建方法来测试或将具有两个参数的路由添加到路由模板。贴出生成的href属性的值。
  • @Eatos 我已将 Startup.cs 文件中的路由添加到帖子中。我没有看到任何生成的 href
  • @coolhand 我正在尝试在示例 .net core mvc 项目中重新创建。我在 ModelsListViewModel 中收到“PagingInfo”类型的错误。您是否为此安装了 nuget 包,或者您的项目中是否为此设置了一个类?
  • @AntiqTech PagingInfo 是一个单独的类,用于创建分页链接,存储在我的 ViewModels 文件夹中。我已经编辑了我的帖子以添加该课程
  • 这是我的示例数据:imgur.com/a/0KJrfBo。我没有使用视图组件,而是对视图代码进行了更改:@model ModelsListViewModel,foreach(Model.Models 中的字符串类型 .Select(x => x.ProgramType) .Distinct() .OrderBy(x => x) ) .在这些更改之后,我得到了这个输出:imgur.com/a/3U8ZT3t 当悬停在它上面时,我可以看到底部的链接。你能检查一下截图看看这是否有帮助吗?

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

问题似乎在于您生成链接 URL 的方式。生成的 URL 类似于 /home/index/my-type/1,其中 my-type 是模型的假定值。

由于您在服务器中的映射需要以单词 Page 为前缀的页面,因此您永远不会找到匹配的路由。您需要将创建链接的代码更改为:

<!-- 
Check modelPage value is now "Page1". Here is the change which generates the URL like
"/home/index/my-type/Page1" as you mapped server side.
-->
<a class="btn btn-block btn-outline-secondary"
           asp-action="Index" asp-controller="Home"
           asp-route-type="@type"
           asp-route-modelPage="Page1">
            @type
        </a>

另外,请确保导入标签助手

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

【讨论】:

  • 为了扩展您的示例,它设置为“/home/index/?modelPage=2”和“home/index/Page2”都返回相同的视图。问题是,正如我在原始帖子中所写,按钮似乎没有生成 href
  • 问题是 ASP.NET 没有找到与“modelPage”参数的参数值匹配的路由,因为您没有以“Page”字符串为前缀。由于没有路由计算单个数字“1”,因此不会生成 URL,因为 ASP.NET 不知道该做什么。
  • 这实际上只是我的_ViewImports.cshtml 文件中的拼写错误。谢谢你帮我找到它。
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
相关资源
最近更新 更多