【发布时间】: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