MVC | Linq Update Query | Help!

Posted by 109221793 on Stack Overflow See other posts from Stack Overflow or by 109221793
Published on 2011-01-30T23:00:38Z Indexed on 2011/01/30 23:26 UTC
Read the original article Hit count: 192

Filed under:
|
|
|
|

Hi guys,

I'm making modifications to a C# MVC application that I've inherited.

I have a database, and for simplicity I'll just focus on the two tables I'm working with for this linq query.

Item
ItemID Int PK
ItemName
RepairSelection (Yes or No)
RepairID Int FK

Repair
RepairID Int PK
RepairCategory
SubmissionDate
DateSentForRepair

Ok, so ItemID is pretty much the identifier, and the View to display the Repair details goes like this (snippet):

            <%= 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})%>

Here is the GET Edit action:

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());
        }

This is also fine, the GET Edit view displays the right details. Where I'm stuck is the linq query to update the Repair table. I have tried it so many ways today that my head is just fried (new to Linq as you may have guessed). My latest try is here (which I know is way off so go easy ;-) ):

 [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 });
        }

For the above attempt I get a Sequence Contains No Elements error.

Any help or pointers would be appreciated. Thanks guys.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc