Click here to Skip to main content
14,267,388 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am a newbie to C# and have inherited a web app that I am now solely responsible for fixing and enhancing. I just created a Controller and a view via the VS wizard "MVC 5 Controller with views, using Entity Framework". I am using the default code that VS generated.

I also noticed that the record still comes up even if the ID is the same one as in the SQL View.

Any help would be greatly appreciated!


@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.JudgmentID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LoanID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.JudgmentAmount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.JudgmentDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

1050001010    1000.00 7/5/2016 12:00:00 AM    Edit | Details | Delete
1050001010    1000.00 7/5/2016 12:00:00 AM    Edit | Details | Delete

The second record should be different.

I get this error in a log.

SOURCE:System.Core TRACE: at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__2[TResult](IEnumerable1 sequence) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)


What I have tried:

I have tried to start over and build everything a second time.

I have ran a debug session and in the controller, it is duplicated so index.chtml is only showing what the controller is getting.
Posted
Updated 12-Aug-19 11:14am
v2
Comments
F-ES Sitecore 12-Aug-19 11:13am
   
You haven't posted the code for the controller so it's impossible to say what is happening or what your code is doing. You get that error when you expect zero or one items in a collection but there are two or more.
Member 14557066 12-Aug-19 11:22am
   
Sorry, did this on a few sites and forgot which ones I added this.

public ActionResult Index(int? id)
{

if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.ID = id;
return View(db.JDs.Where(x=>x.ID ==id).ToList());
}
F-ES Sitecore 12-Aug-19 11:34am
   
You have more than one record with that ID so you get that error. If the data is wrong and the id should be unique then delete the bad row and work out how it got there. If it *is* possible to have multiple IDs then your system is probably flawed in some way.
Member 14557066 12-Aug-19 13:50pm
   
OK, maybe I am doing it wrong. I am just trying to list all records for an LNID, LNID is not the PK of the second table in the SQL View JDID is and LN.ID is the primary for the first table. My SQL view looks like this, basically
select LN.ID, JD.JDID, JD.JDAMT, JD.JDDATE from
LNs
inner Join JD on JD.LNID = LN.LNID

It is supposed to have many LNID records only one ID record but the ID passed to the ActionResult is the ID, not the LNID. How do I get this result? Sorry I did not create this mess I just have to support it and enhance it. Thanks for all the help!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I added
[Key][Column(Order = 0)]
and
[Key][Column(Order = 1)]
to the Model.

So the view would know that the Unique Key was the second column and the first was the PK from the in-joined table and the parameter passed to the view.

Thanks to F-ES Sitecore for pointing me in the right direction!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100