繁体   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