简体   繁体   English

如何从 model 遍历数组

[英]How to iterate over an array from a model

I have an API which returns a reponse like this:我有一个 API 返回这样的响应:

{
  "count": 1,
  "value": [
    {
      "id": "20",
      "url": "https://dev.azure.com/xxx/_apis/wit/workItems/20"
    },

    {
      "id": "21",
      "url": "https://dev.azure.com/xxx/_apis/wit/workItems/20"
    }
  ]
}

I am mapping the response to a C# model like this:我正在将响应映射到 C# model,如下所示:

namespace TestApp.Models
{
  public class TestSuiteModel 
  {
    public int count { get; set; }
    public List<Value> value { get; set; }
  }

  public class Value
  {
    public string id { get; set; }
    public string url { get; set; }
  }
}

I initialize the model in my index.razor page as:我将index.razor页面中的 model 初始化为:

protected override async Task OnInitializedAsync()
{
  await this.GetTestSuites();
}


TestSuiteModel TestSuitesResult;
protected async Task GetTestSuites()
{
  // url = ... 
  TestSuitesResult = await TestSuiteService.GetTestSuites(url);
  await base.OnInitializedAsync();
}

TestSuitesResult works fine as of this step, in that I can do a foreach loop and see the json reponse displayed on my blazor page. TestSuitesResult在这一步工作正常,因为我可以执行一个 foreach 循环并看到 json 响应显示在我的 blazor 页面上。

The only issue I have is when I use the library: https://github.com/Postlagerkarte/blazor-dragdrop我唯一的问题是当我使用库时: https://github.com/Postlagerkarte/blazor-dragdrop

The library says to use the drag and drop service as:图书馆说将拖放服务用作:

<Dropzone Items="MyItems">
  <div>@context.MyProperty</div>
</Dropzone>

MyItems is a sample array and MyProperty is an array key. MyItems是一个示例数组, MyProperty是一个数组键。

But when I use it like this, it doesn't work:但是当我这样使用它时,它不起作用:

<Dropzone Items="TestSuitesResult">
  <div>@context.value</div>
</Dropzone>

I get an error saying TestSuitesResult isn't valid.我收到一条错误消息,指出 TestSuitesResult 无效。 I don't know how else to pass the array into the component我不知道如何将数组传递到组件中

When the Dropzone component is rendered, it's possible the TestSuitesResult object isn't yet populated.当呈现Dropzone组件时,可能尚未填充TestSuitesResult object。 You also aren't targeting the List object. Try the implementation below:您也没有针对列表 object。请尝试以下实现:

@if(TestSuitesResult != null)
    {
      <Dropzone Items="TestSuitesResult.value">
          <div>@context.url </div>
      </Dropzone>
    }

I think Dropzone component is trying to use the entire TestSuitesResult object as an array of items, but it expects an array of values.我认为 Dropzone 组件试图将整个TestSuitesResult object 用作项目数组,但它需要一个值数组。 You should assign the value property of the TestSuitesResult object to the Items property of the Dropzone component instead:您应该将TestSuitesResult object 的 value 属性分配给 Dropzone 组件的Items属性:

<Dropzone Items="TestSuitesResult.value">
<div>@context.id</div>
</Dropzone>

Also, it's possible that the TestSuitesResult object isn't yet populated when the Dropzone component is rendered.此外,在呈现 Dropzone 组件时,可能尚未填充TestSuitesResult object。 To solve this issue, you can consider using a loading indicator to only render the Dropzone component after the TestSuitesResult object has been populated.要解决此问题,您可以考虑使用加载指示器仅在填充了 TestSuitesResult object 之后才呈现 Dropzone 组件。

@if (TestSuitesResult != null)
{
 <Dropzone Items="TestSuitesResult.value">
 <div>@context.value</div>
 </Dropzone>
}
else
{
  // ... loading
}

try this尝试这个

List<Value> TestSuitesResult;
protected async Task GetTestSuites()
{
  // url = ... 
  var model = await TestSuiteService.GetTestSuites(url);
  TestSuitesResult = model.value;

  await base.OnInitializedAsync();
}

The dropzone component actually expects you to provide a property which implements IList<T> dropzone 组件实际上希望您提供一个实现IList<T>的属性

You can see this when pressing F12 within Visual Studio on the parameters name.在 Visual Studio 中的参数名称上按 F12 时,您可以看到这一点。 Or within the code on Github.或者在 Github 的代码内。

public IList<TItem> Items { get; set; }

So all you'll need to do is to provide your Values to the component.因此,您需要做的就是向组件提供您的值。

@if (TestSuitesResult != null)
{
    <Dropzone Items="TestSuitesResult.value">
        <div>@context.id</div> @* access properties from Value class here *@
    </Dropzone>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM