英文:
How to pass array using asp-route?
问题
Form
返回的 URL 格式:localhost2343/index?Colors=red&Colors=blue&Colors=pink
asp-route
返回的 URL 格式:localhost2343/index?Colors=red,blue,pink
如果我使用 form
提交按钮,一切都看起来很好。但是如果我点击 sort
超链接,它会传递 URL Colors=System.String%5B%5D
我如何将 Colors 的值传递到 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 格式
// 从 URL 获取值并设置到 SelectList 中
var result = Request.Query["Colors"];
var i = 0;
foreach (string? item in result)
{
Colors[i] = item;
i++;
}
}
更新 - 我尝试了这个,但在 sort
链接上,它删除了 Sort 变量,并且只选择了一个 Colors(而不是多个):
<a asp-page="./Index" method="get"
asp-route-SortOrder="@Model.Colors_Sort"
asp-all-route-data="@Model.routeData">
<!-- ... -->
</a>
[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());
}
希望这可以帮助你解决问题。
英文:
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());
}
答案1
得分: 1
这可能有所帮助:
要从Razor页面中的选择元素中获取所选值,您可以使用 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中的查询字符串以包含所选值(red, blue, pink),您可以尝试以下代码:
public IActionResult OnGet(string? Colors)
{
if (string.IsNullOrWhiteSpace(Colors))
{
// 做某事
return Page();
}
if (!string.IsNullOrWhiteSpace(Colors))
{
// 做某事
return Page();
}
return Page();
}
public IActionResult OnPostGetColors()
{
IDictionary<string, string> parameters = new Dictionary<string, string>();
var selectedColors = string.Join(",", Request.Form["colors"]);
parameters.Add("Colors", selectedColors);
string query = "";
foreach (var param in parameters)
query += $"{param.Key}={param.Value}";
var url = $"{HttpContext.Request.Path}?{query}";
return Redirect(url); // url : https://localhost:7272/index?Colors=red,blue,pink
}
此代码将帮助您通过URL作为查询字符串提交所选值,然后将用户重定向到包含所选值的新URL,这些值作为颜色参数的多个值添加到查询字符串中。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论