C# ViewComponent导航按钮不';行不通
我正在使用.NET核心MVC。我试图创建的导航链接似乎无法正常工作。我有以下型号:C# ViewComponent导航按钮不';行不通,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我正在使用.NET核心MVC。我试图创建的导航链接似乎无法正常工作。我有以下型号: public class Model { public long ModelID { get; set; } public string Name { get; set; } public string SolverType { get; set; } public string ProgramType { get; set; }
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; }
}
当页面呈现时,将显示视图组件按钮,但将鼠标悬停在上方时链接不起作用或不显示。他们似乎没有在控制器内调用任何操作
编辑:这是我的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);
}
问题似乎出在生成链接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>
这些路由是在
Startup.cs
文件的路由表中定义的吗?此控制器的操作是否可直接路由?用于路由的Asp标记帮助程序使用路由表从您拥有的和平中生成URL。默认管线仅定义一个可选参数。尝试使用一个int参数生成方法进行测试,或者将一个带有两个参数的路由添加到路由模板中。发布生成的href
属性的值。@Eatos我已将Startup.cs文件中的路由添加到发布中。我没有看到任何东西generated@coolhand我试图在一个.net核心mvc项目的示例中重新创建。ModelsListViewModel中的“PaginInfo”类型出现错误。您的项目中是否为此安装了nuget软件包或为此安装了一个类?@AntiqTechPaginInfo
是一个单独的类,用于创建分页链接,存储在my ViewModels文件夹中。我已经编辑了我的帖子来添加这个类。这是我的示例数据:。我没有使用viewcomponent,而是对视图代码进行了更改:@model ModelsListViewModel,foreach(model.Models.Select(x=>x.ProgramType).Distinct().OrderBy(x=>x)中的字符串类型)。在这些更改之后,我得到了以下输出:当鼠标悬停在链接上方时,我可以看到底部的链接。您可以查看屏幕截图以查看这是否有帮助吗?为了扩展您的示例,将其设置为“/home/index/?modelPage=2”和“home/index/Page2”都返回相同的视图。问题是,正如我在原始帖子中所写,按钮似乎没有生成任何href。问题是ASP.NET找不到与“modelPage”参数的参数值匹配的路由,因为您没有使用“Page”字符串作为前缀。由于没有路由匹配单个数字“1”,因此不会生成URL,因为ASP.NET不知道该做什么。实际上,这只是我的\u ViewImports.cshtml
文件中的一个拼写错误。谢谢你帮我找到它。
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);
}
<!--
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