【问题标题】:MVC | Linq Update Query | Help!MVC | Linq 更新查询 |帮助!
【发布时间】:2011-01-30 23:00:38
【问题描述】:

我正在对我继承的 C# MVC 应用程序进行修改。

我有一个数据库,为了简单起见,我将只关注我正在处理这个 linq 查询的两个表。

物品
ItemID Int PK
项目名称
RepairSelection(是或否)
RepairID Int FK

修复
RepairID Int PK
维修类别
提交日期
DateSentForRepair

好的,所以 ItemID 几乎是标识符,显示修复详细信息的视图如下所示 (sn-p):

            <%= Html.LabelFor(x => x.ItemID)%>
            <%= Html.DisplayFor(x => x.ItemID)%><br />
            <%= Html.LabelFor(x => x.Repair.RepairCategory)%>
            <%= Html.DisplayFor(x => x.Repair.RepairCategory, "FormTextShort")%><br />
            <%= Html.LabelFor(x => x.Repair.SubmissionDate)%>
            <%= Html.DisplayFor(x => x.Repair.SubmissionDate)%><br />
            <%= Html.LabelFor(x => x.Repair.DateSentForRepair)%>
            <%= Html.DisplayFor(x => x.Repair.DateSentForRepair)%><br />

<%= Html.ActionLink("Edit Repair Details", "Edit", new { ItemID= Model.ItemID})%>

这是 GET 编辑操作:

public ActionResult Edit(Int64? itemId)
        {
            ModelContainer ctn = new ModelContainer();
            var item = from i in ctn.Items where i.ItemID == itemId select i;
            return View(item.First());
        }

这也很好,GET Edit 视图显示正确的详细信息。我卡住的地方是更新修复表的 linq 查询。今天我尝试了很多方法,以至于我的头都炸了(你可能已经猜到了 Linq 的新手)。我最近的尝试就在这里(我知道这还很遥远,所以放轻松;-)):

 [HttpPost]
        public ActionResult Edit(Int64 itemId, Repair repair, Item item, FormCollection formValues)
        {
            if (formValues["cancelButton"] != null)
            {
                return RedirectToAction("View", new { ItemID = itemId });
            }

            ModelContainer ctn = new ModelContainer();

            Repair existingData = ctn.Repair.First(a => a.RepairId == item.RepairID && item.ItemID == itemId);

            existingData.SentForConversion = DateTime.Parse(formValues["SentForConversion"]);

            ctn.SaveChanges();

            return RedirectToAction("View", new { ItemID = itemId });
        }

对于上述尝试,我得到一个 Sequence Contains No Elements 错误。

任何帮助或指针将不胜感激。谢谢大家。

【问题讨论】:

    标签: c# asp.net-mvc linq controller


    【解决方案1】:

    我自己解决了这个问题。我只是使用不同的方法。当我现在单击编辑操作链接时,我会保留repairID 而不是itemID,然后直接编辑repair 条目。

    【讨论】:

      【解决方案2】:

      看起来 Items 表中的 repairID 缺少 Repair 表中的相应行,

      使用以下查询检查数据:

      SELECT RepairID FROM Items
      EXCEPT
      SELECT RepairID FROM Repair
      

      这将列出修复表中所有缺失的repairID。

      如果需要更正案例数据或您需要使用 FirstOrDefault 并检查返回值可空性,即:

      Repair existingData = ctn.Repair.FirstOrDefault(a => a.RepairId == item.RepairID && item.ItemID == itemId);
      
          if( existingData!= null)
          {
             existingData.SentForConversion = DateTime.Parse(formValues["SentForConversion"]);
             ctn.SaveChanges();
          }
      

      【讨论】:

      • 感谢您的回复。我刚刚更新了我的问题。我应该提到,在 Item 表中,我有一个“RepairSelection”列,它可以是“是”或“否”。当创建初始修复条目时,它会更新为“是”。我通过这个条件过滤了我的项目列表,所以数据肯定在那里。我希望这能更清楚地说明情况:)
      • 如果数据确实存在,那么接下来要检查的是发布的值,即分配给 Edit 操作的 Item 参数的值是什么。你检查了吗?
      • 是的,我已经调试过了代码。它正确地获取了发布的值,但是在我的 Linq 查询中发生了 null 异常。 existingData 为空,这就是为什么我认为我编写查询的方式存在问题。你怎么看?
      • 错误“序列不包含元素”肯定是因为过滤器“a => a.RepairId == item.RepairID && item.ItemID == itemId”没有返回任何行。我很确定应用于过滤器的值产生 0 行。
      • 嗯。明天早上我将不得不重新审视它。我还尝试了该 SQL 查询以在修复表中显示缺少的修复 ID,并且有 0 行,所以我很难过。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      相关资源
      最近更新 更多