【发布时间】: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);
}
}
【问题讨论】: