如何通过锚标记帮助器传递值数组

如何通过锚标记帮助器传递值数组

本文介绍了如何通过锚标记帮助器传递值数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用 Asp.Net Core 2.1 页面

public async Task OnGetAsync([FromQuery(Name = "status")] string[] myValues)
{

}

我正试图从锚标签帮助程序中传递一系列值,但是我在 myValues 中收到的只是一项。如果我手动键入查询字符串以镜像 MyRoute?myValues = A& myValues = B ,它将按预期工作。我尝试了以下操作,但没有成功。

I'm trying to pass in an array of values from an anchor tag helper, but all I receive in myValues is one item. If I manually type out the query string to mirror MyRoute?myValues=A&myValues=B, it works as expected. I've tried the following with no success.

<a asp-page="/MyPage" asp-route-status="A" asp-route-status="B">Click</a>

<a asp-page="/MyPage" asp-route-status="new [] { A, B }">Click</a>

<a asp-page="/MyPage" asp-route-status="@(new [] { A, B })">Click</a>

@{  var list = new string[] { "A", "B" };}
<a asp-page="/MyPage" asp-route-status="@list">Click</a>


推荐答案

这似乎不可能-值传递给 asp-route-status 的值应该是字符串或值,然后无论如何都将其转换为字符串。您可以在,其中 RouteValues 属性是< string,string> 的字典。

This doesn't appear to be possible - The value passed in to asp-route-status is expected to either be a string or a value that it is then converted to a string anyway. You can see this in the source code, where the RouteValues property is a dictionary of <string, string>.

我们来看第一个示例,如下所示:

Let's look at your first example, which looks like this:

<a asp-page="/MyPage" asp-route-status="A" asp-route-status="B">Click</a>

我正在使用Visual Studio及其所有的智能知识-当我使用此代码段时,我是通过绿色的波浪状和工具提示警告属性'asp-route-status'已经存在。 。最终结果是,实际上只有指定的第一个值才传递到查询字符串中。

I'm using Visual Studio with all its intellisense magic - when I use this code snippet, I am warned via a green squiggly and tooltip that the "Attribute 'asp-route-status' already exists.". The end result here is that only the first value specified is actually passed into the query-string.

使用任一有效数组方法,生成的<$ c值$ c> status 只是数组 System.String%5B%5D ToString

With either of your valid array approaches, the generated value for status is simply a ToString of the array - System.String%5B%5D.

似乎要实现您想要的目标,您将需要使用传统的 UrlHelper 方法。看起来像这样:

It appears that in order to achieve what you want, you're going to need to fallback to the traditional UrlHelper approach. Here's what that looks like:

<a href="@Url.Page("/MyPage", new { status = new [] { "A", "B" } })">Click</a>

我发现 Github问题,最近的评论(2017-11-24)询问如何使用标签帮助程序实现此功能,但至今尚未回答该问题(2018-08-10)。

I found this Github issue where the most recent comment (2017-11-24) asks about how to achieve this with a tag-helper, but the question has not been answered as of today (2018-08-10).

这篇关于如何通过锚标记帮助器传递值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:27