【问题标题】:Blazor EditForm bind from ListBlazor EditForm 从列表绑定
【发布时间】:2020-01-18 21:59:47
【问题描述】:

我正在尝试制作一个页面来编辑客户数据。

客户对象有一个 phonenumbers(string) 列表,因为大多数都有固定电话和手机。 我似乎找不到将其放入编辑表单的方法。我尝试使用 foreach 循环,但它不能绑定到这个。 我还尝试在循环中使用本地副本并绑定到该副本。那行得通,但是在按下提交按钮后我无法检索更改。 我究竟做错了什么 ?这样做的正确方法是什么?我似乎找不到任何涵盖此内容的教程。

我已将我的页面重新创建到执行相同操作的最低限度:

这是我的客户类

public class Customer
    {
        public string Name { get; set; }
        // arbitrary extra fields
        public List<string> phoneNumber { get; set; }
    }
}

 public class CustomerService
    {
        Customer jeff;

        public CustomerService()
        {
            jeff = new Customer
            {
                Name = "Jeff",
                phoneNumber = new List<string> { "123456", "654321" },
            };
        }

        public Customer getCustomer()
        {
   
            return jeff;
        }

        public void setCustomer(Customer cust)
        {
            jeff = cust;
        }
    }

还有我的页面

<EditForm Model="@customer" OnSubmit="@submitChanges">

    <InputText id="name" @bind-Value="@customer.Name" /><br/>
    <!-- How do i link the multiple phonenumbers-->

    @foreach(string phone in customer.phoneNumber)
    {
        //this does not compile
        //<InputText @bind-Value="@phone"/>

        //this compiles but i can't find how to acces the data afterward ???
        string temp = phone;
        <InputText @bind-Value="@temp"/>

    }

    @for(int i=0;i<customer.phoneNumber.Count();i++)
    {
        //this compiles but chrashed at page load
        // <InputText @bind-Value="@customer.phoneNumer[i]"/>
    }


    <button type="submit">submit</button>
      
</EditForm>
代码部分
@code {        

    Customer customer;
   
    protected override void OnInitialized()
    {
        customer = _data.getCustomer();           
    }

    private void submitChanges()
    {
        _data.setCustomer(customer);
    }
}

【问题讨论】:

    标签: forms blazor


    【解决方案1】:

    @Wolf,今天我读到了ObjectGraphDataAnnotationsValidator,它被用来代替 DataAnnotationsValidator 组件

    验证绑定模型的整个对象图,包括 集合和复杂类型的属性

    强调包括收藏。因此,我搜索了一个在 EditForm 中实现集合的示例,但找不到。经过一番努力,我成功地做到了。代码如下:

    @page "/"
    @using Microsoft.AspNetCore.Components.Forms
    @using System.ComponentModel.DataAnnotations;
    
    <EditForm Model="@customer" OnSubmit="@submitChanges">
        <DataAnnotationsValidator />
        <p>
            <InputText id="name" @bind-Value="customer.Name" /><br />
        </p>
        @foreach (var phone in customer.phones)
        {
            <p>
                <InputText @bind-Value="phone.PhoneNumber" /> 
            </p>
    
         }
    
         <p>
             <button type="submit">submit</button>
          </p>
    
        </EditForm>
    
     <div>
        <p>Edit  customer</p>
    
        <p>@customer.Name</p>
        @foreach (var phone in customer.phones)
        {
            <p>@phone.PhoneNumber</p>
    
        }
    
    </div>
    @code {
    
    Customer customer;
    
    
    
    protected override void OnInitialized()
    {
        customer = new Customer();
    
    }
    private void submitChanges()
    {
        // _data.setCustomer(customer);
    }
    
    public class Customer
    {
        public string Name { get; set; } = "jeff";
        //[ValidateComplexType]
        public List<Phone> phones { get; } = new List<Phone>() { new Phone 
         {PhoneNumber = "123456" }, new Phone {PhoneNumber = "654321" }};
    }
    
    public class Phone
    {
        public string PhoneNumber { get; set; }
    }
    
    }
    

    希望这会有所帮助...

    【讨论】:

    • 谢谢,这正是我所担心的。在实际应用中,我使用的是显示模型,我会按照您的建议进行操作。
    • 我最终在显示模型中使用了一个字符串。用换行符和页面中的 InputTextArea 分隔每个。似乎工作得很好。再次感谢您的回复。
    • 查看您现在提供的代码,我知道我做错了什么。我试图绑定到 foreach 循环中的迭代器。但这无法改变。当链接到 iterator 的子元素时,它确实有效。我永远不会自己找到这个。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2021-06-16
    • 2021-07-23
    • 2022-11-18
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多