每次勾選復選框時,我都想為串列項執行DbUpdate 。我的串列是屬性,但當 foreach 中的 @bind 更改時,它不會觸發 SET。我也不能在 foreach 上使用 @Bind 并同時使用 @Onchanged。我怎樣才能實作我的目標,最好的 Blazor 實踐是什么?
Foreach: 第一個復選框處理方法,但傳遞的物件沒有被“檢查”更改第二個復選框更改物件的布林值,但我不能將 @Onchange 與 @Bind 結合使用
@foreach (var configuration in dashboardConfigurations)
{
<tr>
<td>@configuration.Customer.Name</td>
<td>@configuration.Customer.Vat</td>
<td><input type="checkbox" @onchange="@(async () => await SaveConfiguration(configuration))" checked="@configuration.UploadPurchaseInvoices"/></td>
<td><input type="checkbox" @bind="configuration.UploadSalesInvoices" /></td>
<td><button @onclick="@(()=> TestMethod(1))">Disable Customer</button></td>
</tr>
}
對于: 希望文本框的更改會呼叫我的引數 List 的 SET 但它什么也不做......
@for(int i = 0; i < DashboardConfigurations.Count; i ){
<tr>
<td>@DashboardConfigurations[i].Customer.Name</td>
<td>@DashboardConfigurations[i].Customer.Vat</td>
<td><input type="checkbox" @bind="DashboardConfigurations[i].UploadPurchaseInvoices"/></td>
<td><input type="checkbox" @bind="DashboardConfigurations[i].UploadSalesInvoices" /></td>
<td><button @onclick="@(()=> TestMethod(1))">Disable Customer</button></td>
</tr>
}
代碼:
@code {
private List<CustomerDashboardConfiguration> dashboardConfigurations;
[Parameter]
public List<CustomerDashboardConfiguration> DashboardConfigurations
{
get { return dashboardConfigurations; }
set { dashboardConfigurations = value; }
}
internal async Task SaveConfiguration(CustomerDashboardConfiguration customerDashboardConfiguration)
{
Console.WriteLine("Saving object");
}
}
編輯:這有效,這正是我想要實作的,但在我的 foreach 中它非常難看:
<td><input type="checkbox" @onchange="@(async e =>{configuration.UploadPurchaseInvoices = (bool)e.Value; await SaveConfiguration(configuration);})" checked="@configuration.UploadPurchaseInvoices"/></td>
uj5u.com熱心網友回復:
布賴恩已經回答了你問題的第一部分。
這很有效,這正是我想要實作的目標,但在我的 foreach 中它非常難看。
<td><input type="checkbox" @onchange="@(async e =>{configuration.UploadPurchaseInvoices = (bool)e.Value; await SaveConfiguration(configuration);})" checked="@configuration.UploadPurchaseInvoices"/></td>
當然是!將匿名方法移動到代碼部分并呼叫該方法。
<td>
<input
type="checkbox"
@onchange="(e) => CheckboxChanged(configuration, e)"
checked="@configuration.UploadPurchaseInvoices"
/>
</td>
@code {
//....
async Task CheckboxChanged(DashboardConfiguration configuration, ChangeEventArgs e)
{
configuration.UploadPurchaseInvoices = (bool)e.Value;
await SaveConfiguration(configuration);
}
}
希望有一種方法可以使用系結物件,因此不再需要 (bool)e.Value ,但這可以!
有。請參閱下面的代碼。但是代碼說明的事件計時存在問題。
@page "/counter"
<div>
<input type="checkbox" @bind-value=Checked @oninput=Changed /> CheckBox
</div>
<div class="bg-dark text-white p-2 m-2">
Checked : @Checked
</div>
<div class="alert alert-secondary m-2">
Message : @message
</div>
@code {
private bool Checked { get; set; }
private string message = string.Empty;
private void Changed( ChangeEventArgs e )
{
message = $"Clicked at {DateTime.Now.ToLongTimeString()}. Sumitted Value is {e.Value?.ToString()} Checked Property was {Checked}.";
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505433.html
