如何在www.example.com的URL中传递可选的多个值asp.net?

hjzp0vay  于 2023-01-10  发布在  .NET
关注(0)|答案(1)|浏览(168)

如何在链接的URL中传递多个可选值?
我能够使它的工作通过使用以下方法,但问题是,如果有大约15列和15过滤器在网格中。如果你必须添加第16过滤器比你将不得不添加代码行16次在下面的例子。或者如果你必须删除第16过滤器比你将结束删除16行代码。

<a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort" 
   asp-route-SearchString="@Model.SearchString"
   asp-route-First_Name="@Model.First_Name"
   asp-route-Last_Name="@Model.Last_Name"
   asp-route-Location="@Model.Location">`

如果我不包括链接内的URL变量比它会失去过滤器的值。也许一个模型对象将工作?

<a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort" >

排序显示数据

<table>
    <thead>
        <a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort" 
           asp-route-SearchString="@Model.SearchString"
           asp-route-First_Name="@Model.First_Name"
           asp-route-Last_Name="@Model.Last_Name"
           asp-route-Location="@Model.Location">
                        @Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
         </a>
        <a asp-page="./Index" asp-route-SortOrder="@Model.Last_Name_Sort" 
           asp-route-SearchString="@Model.SearchString"
           asp-route-First_Name="@Model.First_Name"
           asp-route-Last_Name="@Model.Last_Name"
           asp-route-Location="@Model.Location">
                        @Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
         </a>
         ... //another 15 columns 
    <thead>
    ...
 </table>

下面我有4个过滤器

<input type="text" asp-for="SearchString" />
<input type="text" asp-for="First_Name" />
<input type="text" asp-for="Last_Name" />
<input type="text" asp-for="Location" />
.. more filters

后端代码:

[BindProperty(SupportsGet = true)]
public string? SearchString { get; set; }
[BindProperty(SupportsGet = true)]
public string? First_Name { get; set; }
[BindProperty(SupportsGet = true)]
public string? Last_Name { get; set; }
[BindProperty(SupportsGet = true)]
public string? Location { get; set; }

public async Task OnGetAsync()
{
}
jljoyd4f

jljoyd4f1#

使用asp-all-route-data属性并传递字典(https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#notes):

@{
var routeData = new Dictionary<string, string>{
    { "SortOrder", Model.First_Name_Sort },
    { "SearchString", Model.SearchString },
    { "First_Name", Model.FirstName }
    { "Last_Name", Model.Last_Name },
    { "Location", Model.Location}
};
}

然后

<a asp-page="./Index" asp-all-route-data="routeData">
    @Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
</a>
<a asp-page="./Index" asp-all-route-data="routeData">
    @Html.DisplayNameFor(model => model.CourseTakenList[0].Last_Name)
</a>
... //another 15 columns

相关问题