我正在跟踪Microsoft文档中的“使用ASP.NET Core中的数据”教程,(排序,过滤器,页面和组部分),但是我很难理解以下代码有效...(我是剃刀页面和asp.net核心的新手。)
这是学生索引页面的PageModel。
namespace Contoso.Pages.Students
{
public class IndexModel : PageModel
{
//
public string NameSort { get; set; }
public string DateSort { get; set; }
public string CurrentFilter { get; set; }
public string CurrentSort { get; set; }
public async Task OnGetAsync(string sortOrder,string currentFilter, string searchString, int? pageIndex)
{
CurrentSort = sortOrder;
NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
DateSort = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
pageIndex = 1;
}
else
{
searchString = currentFilter;
}
CurrentFilter = searchString;
IQueryable<Student> studentIQ = from s in _context.Students
select s;
if (!String.IsNullOrEmpty(searchString))
{
studentIQ = studentIQ.Where(s => s.LastName.Contains(searchString)
|| s.FirstMidName.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
studentIQ = studentIQ.OrderByDescending(s => s.LastName);
break;
case "Date":
studentIQ = studentIQ.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
studentIQ = studentIQ.OrderByDescending(s => s.EnrollmentDate);
break;
default:
studentIQ = studentIQ.OrderBy(s => s.LastName);
break;
}
}
}
}
这是模型索引页面中的from。
@*other markup commented out*@
<table class="table">
<thead>
<tr>
<th>
<a asp-page="./Index" asp-route-sortOrder="@Model.NameSort"
asp-route-currentFilter="@Model.CurrentFilter">
@Html.DisplayNameFor(model => model.Student[0].LastName)
</a>
</th>
<th>
@Html.DisplayNameFor(model => model.Student[0].FirstMidName)
</th>
<th>
<a asp-page="./Index" asp-route-sortOrder="@Model.DateSort"
asp-route-currentFilter="@Model.CurrentFilter">
@Html.DisplayNameFor(model => model.Student[0].EnrollmentDate)
</a>
</th>
<th></th>
</tr>
</thead>
</table>
@*other markup commented out*@
现在我的问题是asp-route-sortOrder="@Model.NameSort"
例如
与sortOrder
方法中的OnGetAsync
参数有关?以及如何通过单击索引页面中的链接来设置值sortOrder
?
答案 0 :(得分:0)
asp-route-sortOrder="@Model.NameSort"
与sortOrder
方法中的OnGetAsync
参数有何关系?
通过选择标记帮助器上的route
属性,您可以指定路线数据参数的值。如果将密钥(在这种情况下为sortOrder
)作为路由模板的一部分包含在内,则该值将在生成的URL中显示为段。否则,它将作为查询字符串值附加到URL。这些通过模型绑定与处理程序方法匹配。您可以在https://www.learnrazorpages.com/razor-pages/model-binding