[英]Blazorise Datagrid inline edit for complex object
I am using Blazorise Datagrid .我正在使用Blazorise Datagrid 。 This extension support inline editing as well.这个扩展也支持内联编辑。 When editing a simple object the inline edit working just fine.当编辑一个简单的对象时,内联编辑工作得很好。 But when i am using object as property in my parent object the update process does not work as expected.但是当我在父对象中使用对象作为属性时,更新过程无法按预期工作。 When i changed the city the value not updated.当我更改城市时,值未更新。 Is there anything I have missed?有什么我错过了吗? Thanks in advance提前致谢
Here is my code这是我的代码
<section class="section">
<div class="container">
<DataGrid Data="forecasts" TItem="WeatherForecast" EditMode="Blazorise.DataGrid.DataGridEditMode.Inline">
<DataGridCommandColumn TItem="WeatherForecast" CellsEditableOnEditCommand="true"></DataGridCommandColumn>
<DataGridDateColumn TItem="WeatherForecast" Field="@nameof(WeatherForecast.Date)" Caption="Date" Editable="true"></DataGridDateColumn>
<DataGridDateColumn TItem="WeatherForecast" Field="@nameof(WeatherForecast.Temperature)" Caption="Temperature" Editable="true"></DataGridDateColumn>
<DataGridSelectColumn TItem="WeatherForecast" Field="@nameof(WeatherForecast.City)" Caption="City" Editable="true">
<DisplayTemplate>
@{
var name = (context as WeatherForecast).City?.Name;
@name
}
</DisplayTemplate>
<EditTemplate>
@{
<Select TValue="City" SelectedValue="@((City)(((CellEditContext)context).CellValue))" SelectedValueChanged="@( v => ((CellEditContext)context).CellValue = v)">
@foreach (var item in Cities)
{
<SelectItem TValue="City" Value="@(item)">@item.Name</SelectItem>
}
</Select>
}
</EditTemplate>
</DataGridSelectColumn>
</DataGrid>
</div>
@code {
private List<WeatherForecast> forecasts;
private List<City> Cities;
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now); // get list of forecast
Cities = await ForecastService.GetCityListAsync(); // get list of cities
}}
Model classes模型类
public class WeatherForecast
{
public int Id { get; set; }
public DateTime Date { get; set; }
public int Temperature { get; set; }
public City City { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
}
Try this workaround since Select component doesn't support complex objects binding:尝试此解决方法,因为 Select 组件不支持复杂对象绑定:
<EditTemplate>
<Select
TValue="int"
SelectedValue="@((int)((City)(((CellEditContext)context).CellValue)).Id)"
SelectedValueChanged="@( v => ((CellEditContext)context).CellValue = Cities.First(x=> x.Id == v))">
@foreach (var item in Cities)
{
<SelectItem TValue="int" Value="@(item.Id)">@item.Name</SelectItem>
}
</Select>
</EditTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.