我正在尝试创建一个编辑客户数据的页面。
客户对象拥有电话号码列表(字符串),因为大多数人都有固定电话和手机。我无法找到一种将其放入编辑表单中的方法。我尝试使用foreach循环,但它无法绑定到此数据。我也尝试在循环中使用本地副本并绑定到该副本。这可以工作,但是我无法在提交按钮被按下后检索更改。我做错了什么?正确的做法应该是什么?我似乎找不到任何涵盖此内容的教程。
我已经将我的页面重新创建为最小的版本,以实现相同的功能:
这是我的Customer类:
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);
}
}