将网格绑定到匿名 LINQ 结果,然后将更改提交到数据库?

发布于 2024-10-20 09:34:25 字数 1717 浏览 1 评论 0原文

我一直在研究如何最好地做到这一点,智慧将不胜感激。出于只读目的,我一直很乐意使用 LINQ 并将其绑定到网格。出于编辑目的,我使用了 LinqDataSource 控件,在此过程中启用了编辑/删除操作,并且我有一个绑定到部分或全部表字段的漂亮的可编辑网格。

现在我遇到一种情况,我想编辑表 A 中的一些字段,但链接表 B 中有各种值我也想在该网格中显示(不编辑这些值)。所以我的查询如下所示。 tblDupes 中的字段(已清除,注释)是我想要编辑的字段,但我想显示那些 tblVoucher 字段。

var theDupes = from d in db.tblDupes
                           where d.dupeGroup == Ref
                           select new
                           {
                               Ref = d.dupeGroup,
                               InvoiceNum = d.tblVoucher.invoiceRef,
                               Value = d.tblVoucher.invoiceAmtDecimal,
                               VendorNum = d.tblVoucher.vendorID,
                               VendorName = d.tblVoucher.vendorName,
                               Cleared = d.Cleared
                               Notes = d.Notes
                           };

一个类似但不同的问题 LINQDataSource - Query Multiple Tables? 让我查看 scott Guthrie 的博客文章< a href="http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt- asp-linqdatasource-gt-control.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a -custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx,他在其中处理各种事件以使 LinqDataSource 具有跨表的自定义查询。不过,这似乎仍然针对显式设计的类,即使该类只有字段的子集。

所以我的问题是:是否有一种简单的方法可以允许提交对匿名集合所做的更改(更改。提交类型操作),或者只是一种简单的方法来“显示”另一个表中的字段而不涉及更新?

编辑:再想一想,它并不一定要匿名。我很乐意定义一个类来包含该查询中的元素,因为它不会经常更改。但是,这些元素将跨两个表,即使只有一个需要更新。不确定这是否表明实体框架会更合适 - 我感觉它不会 - 我不希望整个“模型”总是以这种方式对字段进行分组。

谢谢!

I've been looking into how best to do this and wisdom would be appreciated. For read only purposes, I've been happily using LINQ and binding it to a grid. For editing purposes, I've used the LinqDataSource control, enabled the Edit/Delete operations in the process, and I have a nice editable grid bound to some or all of the table's fields.

Now I have a situation where I want to edit a few fields in table A, but there are various values in linked table B that I want to display in that grid too (no editing of those). So my query looks like the below. The fields in tblDupes (cleared, notes) are what I want to edit, but I'd like to display those tblVoucher ones.

var theDupes = from d in db.tblDupes
                           where d.dupeGroup == Ref
                           select new
                           {
                               Ref = d.dupeGroup,
                               InvoiceNum = d.tblVoucher.invoiceRef,
                               Value = d.tblVoucher.invoiceAmtDecimal,
                               VendorNum = d.tblVoucher.vendorID,
                               VendorName = d.tblVoucher.vendorName,
                               Cleared = d.Cleared
                               Notes = d.Notes
                           };

A similar but different question LINQDataSource - Query Multiple Tables? sent me looking at scott Guthrie's blog entry http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx, where he handles various events to have a LinqDataSource with a custom query across tables. This still seems aimed at explicitly designed classes though, even if the class has only a subset of the fields.

So my question is: is there an easy way to allow committing of the changes made to the anonymous collection (a changes.Submit type action), or just an easy way to 'display' fields from another table while not involving them in the updating?

EDIT: Thinking more, it doesn't have to be anonymous really. I'd be happy to define a class to contain the elements in that query, since it won't change often. But, those elements would be across two tables, even though only one needs updating. Not sure if that suggests entity framework would be more suitable - I get the feeling it wouldn't - I don't want the whole 'model' always grouping the fields in this way.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-10-27 09:34:25

在这里进行大胆的猜测,但是您不能监听 LINQDataSource.Updating 事件并在那里执行保存吗?当然,您可能会遇到一些映射问题,因为您无法在 LinqDataSourceUpdateEventArgs.OriginalObject 中键入 object

如果您创建 ViewModel 而不是匿名类型会怎样?类似于 DupeVoucherViewModel 之类的东西。

然后,在 Updating 事件中,您可以将 LinqDataSourceUpdateEventArgs.OriginalObject 强制转换为 DupeVoucherViewModel 对象,并开始映射和保存数据。

示例

假设您创建了一个名为 DupeVoucherViewModel 的视图模型(类)并像这样绑定到它:

var theDupes = from d in db.tblDupes
                       where d.dupeGroup == Ref
                       select new DupeVoucherViewModel
                       {
                           Ref = d.dupeGroup,
                           InvoiceNum = d.tblVoucher.invoiceRef,
                           Value = d.tblVoucher.invoiceAmtDecimal,
                           VendorNum = d.tblVoucher.vendorID,
                           VendorName = d.tblVoucher.vendorName,
                           Cleared = d.Cleared
                           Notes = d.Notes
                       };

那么服务器标记应该像这样映射更新事件:

<asp:LinqDataSource EnableUpdate="true" OnUpdating="LinqDataSource_Updating" />

并且后面的代码应包含以下方法:

protected void LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
   // originalObject contains the unchanged object
   var originalObject = (DupeVoucherViewModel)e.OriginalObject;
   // newObject contains the changed object
   var newObject = (DupeVoucherViewModel)e.NewObject;

   // Perform your save using newObject
   yourDataAccessComponent.SaveSomething(newObject);
}

您可能需要在 DupeVoucherViewModel 中包含更多信息,例如 tblDupes 的 ID 等,以便您可以加载并更改它。

您可以在此处阅读有关 LinqDataSource 的更多信息: http://msdn.microsoft.com/ en-us/library/bb514963.aspx

Taking a wild guess here, but couldn't you listen to the LINQDataSource.Updating event and perform your save there? You would, of course, have some problems with the mapping since you cannot type the object in the LinqDataSourceUpdateEventArgs.OriginalObject.

What if you create a ViewModel instead of the anonymous type. Something like DupeVoucherViewModel.

Then in the Updating event, you could cast the LinqDataSourceUpdateEventArgs.OriginalObject to the DupeVoucherViewModel object and start mapping and saving your data.

Example

Given that you create a view model (a class) that you call DupeVoucherViewModel and bind to that like so:

var theDupes = from d in db.tblDupes
                       where d.dupeGroup == Ref
                       select new DupeVoucherViewModel
                       {
                           Ref = d.dupeGroup,
                           InvoiceNum = d.tblVoucher.invoiceRef,
                           Value = d.tblVoucher.invoiceAmtDecimal,
                           VendorNum = d.tblVoucher.vendorID,
                           VendorName = d.tblVoucher.vendorName,
                           Cleared = d.Cleared
                           Notes = d.Notes
                       };

Then the server tag should map the updating event like so:

<asp:LinqDataSource EnableUpdate="true" OnUpdating="LinqDataSource_Updating" />

And the code behind should contain the following method:

protected void LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
   // originalObject contains the unchanged object
   var originalObject = (DupeVoucherViewModel)e.OriginalObject;
   // newObject contains the changed object
   var newObject = (DupeVoucherViewModel)e.NewObject;

   // Perform your save using newObject
   yourDataAccessComponent.SaveSomething(newObject);
}

You might need to include some more information in the DupeVoucherViewModel such as an ID of tblDupes or something, so that you can load that and change it.

You can read more about the LinqDataSource here: http://msdn.microsoft.com/en-us/library/bb514963.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文