烈酒&实体框架双向结合
我有一个应用于Blazor Server .NET6的应用程序。使用Blazor Server而不是WebAssembly的原因是因为该站点持有数据库连接。 在其中一个页面上有一个表,上面有许多行,其中最后一列是带有复选框类型的输入。另外,它的行具有ID,以防万一。
我正在尝试将两种方式绑定进行存档,因此,当将复选框设置为True时,应更新数据库记录,相应的行/列将值更改为true。
这可以以优雅的方式完成,例如老式的devexpress或telerik在WPF上使用binding在没有observableCollections的情况下进行绑定,或者需要创建事件功能并发布方法以及LINQ来对此进行归档?
查看代码:
<tbody>
@foreach (var datamodel_structure in datamodel_structureList)
{
<tr>
<td><input type="checkbox" @bind=datamodel_structure.datamodel_structure_is_selected /></td>
</tr>
}
</tbody>
控制器代码:
[HttpGet, Route("structure/{id:int}")]
public async Task<List<DataModel_Structure_Ind>> GetDataModel_Structures(int id)
{
return await Task.FromResult(_IDataModel.GetDataModel_Structures(id));
}
接口代码:
public List<DataModel_Structure_Ind> GetDataModel_Structures(int id);
服务代码:
public List<DataModel_Structure_Ind> GetDataModel_Structures(int id)
{
try
{
return _dbContext.DataModel_Structures_Ind
.OrderBy(x => x.datamodel_structure_id)
.Where(x => x.datamodel_id == id)
.ToList();
}
catch
{
return null;
}
}
I have an application written in Blazor Server .Net6. The reason of using Blazor Server instead of WebAssembly is because the site is holding database connections.
On one of the pages has a table with many rows where the last column is an input with type of checkbox. In addition it's row has an ID in case is needed.
I am trying to archive two way binding, so when a checkbox is set to true, database record should be updated and the corresponding row/column change the value to True.
Can this be done in elegant way, like old school DevExpress or Telerik was doing on WPF with binding without events using ObservableCollections, or is needed to create event functions and post methods and LinQ to archive this?
View Code:
<tbody>
@foreach (var datamodel_structure in datamodel_structureList)
{
<tr>
<td><input type="checkbox" @bind=datamodel_structure.datamodel_structure_is_selected /></td>
</tr>
}
</tbody>
Controller Code:
[HttpGet, Route("structure/{id:int}")]
public async Task<List<DataModel_Structure_Ind>> GetDataModel_Structures(int id)
{
return await Task.FromResult(_IDataModel.GetDataModel_Structures(id));
}
Interface Code:
public List<DataModel_Structure_Ind> GetDataModel_Structures(int id);
Service Code:
public List<DataModel_Structure_Ind> GetDataModel_Structures(int id)
{
try
{
return _dbContext.DataModel_Structures_Ind
.OrderBy(x => x.datamodel_structure_id)
.Where(x => x.datamodel_id == id)
.ToList();
}
catch
{
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您制作了一个自定义的复选框组件,该组件将FieldName/ID作为键,然后具有反映UI所描绘的内容和服务器具有的值该怎么办?复选框OnChange事件可以启动SQL CMD进行更新,然后在DB更新后,您可以在自定义复选框组件中调用statehaschanged()以更新UI以反映更改。
What if you made a custom checkbox component that takes the FieldName/ID as a Key and then has the Value that would reflect what the UI depicts and what the server has? The checkbox onchange event could fire off a SQL CMD to update, then after the DB has been updated you can call StateHasChanged() in the custom checkbox component to update the UI to reflect the changes.