簡體   English   中英

Blazor 綁定一個列表<string>在 EditForm 中</string>

[英]Blazor binding a List<string> in an EditForm

我有一個與這個問題類似的問題,因為我無法將 Blazor EditForm 綁定到一個簡單的列表。

為了將列表綁定到 EditForm,我是否遺漏了一些東西?

個人.cs

public class Person {
  public List<string>? Names { get; set; }
}

EditForm1.razor 產生編譯時錯誤: Cannot assign to 'item' because it is a 'foreach iteration variable' 我明白了 - 迭代器是只讀的,所以我可以理解。

<EditForm Model="@person">
  @if (person is not null) {
    @if (person.Names is not null) {
      @foreach (var item in person.Names) {
        <InputText @bind-Value="@item" />
      }
    }
  }
</EditForm>

因此,根據引用的 Microsoft 文檔,我對其進行了重構。

EditForm2.razor 編譯並運行...直到 person.Names 實際上有一個值。 然后它拋出ArgumentException: The provided expression contains a InstanceMethodCallExpression1 which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object. Microsoft.AspNetCore.Components.Forms.FieldIdentifier.ParseAccessor<T>(Expression<Func<T>> accessor, out object model, out string fieldName) ArgumentException: The provided expression contains a InstanceMethodCallExpression1 which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object. Microsoft.AspNetCore.Components.Forms.FieldIdentifier.ParseAccessor<T>(Expression<Func<T>> accessor, out object model, out string fieldName)

<EditForm Model="@person">
  @if (person is not null) {
    @if (person.Names is not null) {
      @for (int x = 0; x < person.Names.Count; x++) {
        <InputText @bind-Value="@person.Names[x]" />
      }
    }
  }
</EditForm>

EditForm3.razor 是我最后一次嘗試。 這會編譯和渲染,但是一旦我嘗試對編輯框執行任何操作,應用程序就會崩潰,並出現Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') 我 99% 肯定這種方法是錯誤的,但我現在正抓着稻草。

<EditForm Model="@person">
  @if (person is not null) {
    @if (person.Names is not null) {
      @for (int x = 0; x < person.Names.Count; x++) {
        <input @bind="@person.Names[x]" />
      }
    }
  }
</EditForm>

答案在您鏈接到的已接受答案中...

您正在尋找創建集合的雙向數據綁定。

<EditForm Model="@person">
    @foreach (var PName in person.names) 
    {
        <InputText @bind-Value="@PName.Name" />
    }
 </EditForm>

@code
{
private Person person = new Person {ID = "1", names = new List<PersonName>() 
                { new PersonName {Name = "Marry" }, 
                  new PersonName {Name = "Marria" } 
                };

public class Person
{
   public string ID {get;set;}
   public List<PersonName> names { get; set; }
}

public class PersonName 
{
    public string Name { get; set; }
}

} 

請注意,為了綁定 Name 屬性,您必須在其自己的 class 中定義它,並在 Person model 中定義該 class ( PersonName ) 的列表。 這是綁定到集合的唯一方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM