如何使用asp-route传递数组?

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

Form返回url格式:localhost2343/index?Colors=red&Colors=blue&Colors=pink
asp-route返回url格式:localhost2343/index?Colors=red,blue,pink
如果我使用form提交按钮比一切似乎都好。但如果我点击sort超链接比它将通过网址Colors=System.String%5B%5D

如何在asp-route内部传递Colors的值?

<form asp-page="./index" method="get">
     <select asp-for="Colors" asp-items="@Model.Colors_SELECT" class="MultiSelect" multiple>
        <option value="">All</option>
   </select>
    ...
</form>

<Table>
  ...
    <a  asp-page="./Index" method="get" 
        asp-route-SortOrder="@Model.Colors_Sort" 
        asp-route-SearchString="@Model.SearchString"
        asp-route-Colors="@Model.Colors">
           @Html.DisplayNameFor(model => 
    model.MyList[0].Colors)
     </a>
...
</table>

  [BindProperty(SupportsGet = true)]
    public string[]? Colors { get; set; }
    public SelectList? Colors_SELECT { get; set; }

public async Task OnGetAsync()
{
   // Form - URL Format 
   // get values from URL & set inside selectlist 
   var result = Request.Query["Colors"];
   var i = 0;
   foreach (string? item in result) {
          Colors[i] = item;
          i++;
   }
}

**更新-**我尝试了这个,但在sort链接上,它删除了排序变量&它只选择1种颜色(而不是多种)

<a  asp-page="./Index" method="get" 
        asp-route-SortOrder="@Model.Colors_Sort" 
        asp-all-route-data="@Model.routeData">
          
           [BindProperty(SupportsGet = true)]
           public Dictionary<string, string> routeData { get; set; }
             ....
           var routeData = new Dictionary<string, string>();
            routeData.Add("SortOrder", CurrentSort);
            routeData.Add("SearchString", SearchString);
            for (int i = 0; i < result.Count; i++)
            {
              var myParam = result[i];
             routeData.Add($"Colors{i}", myParam.ToString());
            }
velaa5lx

velaa5lx1#

这可能有助于:要从Razor页面中的select元素获取选定值,可以使用Request.Form["selectName"]集合。例如,考虑以下select元素:

@page "{Colors}"

<form method="post">
  <select multiple name="colors">
    <option value="red">Red</option>
    <option value="blue">Blue</option>
    <option value="pink">Pink</option>
  </select>
  <button type="submit" asp-page-handler="GetColors">Submit</button>
</form>

要获取表单提交处理程序中的选定值,修改URL中的查询字符串并包含选定值(红色、蓝色、粉红色),可以尝试以下代码:

public IActionResult OnGet(string? Colors)
{
    if (string.IsNullOrWhiteSpace(Colors))
    {
        // do something
        return Page();
    }

    if (!string.IsNullOrWhiteSpace(Colors))
    {
        // Do something
        return Page();
    }

    return Page();
}

public IActionResult OnPostGetColors()
{
    IDictionary<string, string> params = new Dictionary<string, string>();
    var selectedColors = string.Join(",", Request.Form["colors"]);
    params.Add("Colors", selectedColors);
    string query = "";
    foreach (var p in params)
        query += $"{p.Key}={p.Value}";

    var url = $"{HttpContext.Request.Path}?{query}";
    return Redirect(url); // url : https://localhost:7272/index?Colors=red,blue,pink
}

这段代码将帮助您通过URL将选定的值作为查询字符串提交,然后将用户重定向到新的URL,并将选定的值作为colors参数的多个值添加到查询字符串中。

相关问题