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

huangapple go评论85阅读模式
英文:

How to pass optional multi values in URL in asp.net?

问题

如何在链接中传递多个可选值的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变量,那么它将丢失过滤器的值。也许一个Model对象会起作用?

<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>
         ... //另外的15列
    <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" />
.. 更多过滤器

后端代码:

[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()
{
}
英文:

How to pass multiple, optional values in URL on link?

I was able to make it work by using following approach but issue is that what if there are about 15 columns and 15 filters in grid. If you have to add 16th filter than you will have to add line of code 16 times in below example. Or if you have to remove 16th filter than you will end up removing 16 lines of code.

&lt;a asp-page=&quot;./Index&quot; asp-route-SortOrder=&quot;@Model.First_Name_Sort&quot; 
   asp-route-SearchString=&quot;@Model.SearchString&quot;
   asp-route-First_Name=&quot;@Model.First_Name&quot;
   asp-route-Last_Name=&quot;@Model.Last_Name&quot;
   asp-route-Location=&quot;@Model.Location&quot;&gt;`

If I don't include URL variables inside link than it will lost the values of filters. maybe a Model Object would work?

 &lt;a asp-page=&quot;./Index&quot; asp-route-SortOrder=&quot;@Model.First_Name_Sort&quot; &gt;

Display data with sorting

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

Below I have 4 filters

&lt;input type=&quot;text&quot; asp-for=&quot;SearchString&quot; /&gt;
&lt;input type=&quot;text&quot; asp-for=&quot;First_Name&quot; /&gt;
&lt;input type=&quot;text&quot; asp-for=&quot;Last_Name&quot; /&gt;
&lt;input type=&quot;text&quot; asp-for=&quot;Location&quot; /&gt;
.. more filters 

Backend code:

[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()
{
}

答案1

得分: 1

使用 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>
... // 另外 15 列
英文:

Use the asp-all-route-data attribute and pass a dictionary (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#notes):

@{
var routeData = new Dictionary&lt;string, string&gt;{
    { &quot;SortOrder&quot;, Model.First_Name_Sort },
    { &quot;SearchString&quot;, Model.SearchString },
    { &quot;First_Name&quot;, Model.FirstName }
    { &quot;Last_Name&quot;, Model.Last_Name },
    { &quot;Location&quot;, Model.Location}
};
}

And then

&lt;a asp-page=&quot;./Index&quot; asp-all-route-data=&quot;routeData&quot;&gt;
    @Html.DisplayNameFor(model =&gt; model.CourseTakenList[0].First_Name)
&lt;/a&gt;
&lt;a asp-page=&quot;./Index&quot; asp-all-route-data=&quot;routeData&quot;&gt;
    @Html.DisplayNameFor(model =&gt; model.CourseTakenList[0].Last_Name)
&lt;/a&gt;
... //another 15 columns

huangapple
  • 本文由 发表于 2023年1月9日 01:11:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定