0

Form returns url format: localhost2343/index?Colors=red&Colors=blue&Colors=pink

asp-route return url format: localhost2343/index?Colors=red,blue,pink

If I use form submit button than everything seems good. But If i click on sort hyperlink than it will pass URL Colors=System.String%5B%5D

How can I pass value of Colors inside asp-route?

<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++;
   }
}

Update - I tried this but on sort link, it removes Sort variable & it picks only 1 Colors (not multi)

    <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());
            }
4
  • Why do you want to use a commas separated list instead? Commented Jan 9, 2023 at 2:01
  • form return url without commons and asp-route returns url with commons. just confused why that is and how to go about picking one format Commented Jan 9, 2023 at 2:52
  • 1
    IMHO pass RouteData.Values to asp-all-route-data so you don't have to enumerate each value separately. Commented Jan 9, 2023 at 3:22
  • @JeremyLakeman Thanks, I just searched how to use asp-all-route-data but i think now its removes all URL variables. did I missed some code Commented Jan 9, 2023 at 3:51

1 Answer 1

1

This may help: To get the selected values from a select element in a Razor page, you can use the Request.Form["selectName"] collection. For example, consider the following select element:

@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>

To get the selected values in the form submission handler and modify the query string in the URL and include the selected values (red, blue, pink), you can try the following code:

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
}

This code will help you submit the selected values through the URL as a query string, and then will redirect the user to a new URL with the selected values added to the query string as multiple values for the colors parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

smart solution, I didnt think of doing in OnPost
sorry to bother. I just have a quick add on issue maybe you can help. if not no worries. thanks, https://stackoverflow.com/questions/75061161/how-to-select-values-inside-list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.