Link to home
Start Free TrialLog in
Avatar of Joe Pelish
Joe PelishFlag for United States of America

asked on

Getting dupes in Index View

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!

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

This is the controller.
public ActionResult Index(int? id)
{
 
if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
ViewBag.ID = id;
return View(db.JD.Where(x=>x.ID ==id).ToList());
}

This is the index.cshtml code
@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>
}
This is what is displayed. There are actually 3 different records it should display.
1050001010      10000.00      7/25/2015 12:00:00 AM
1050001010      10000.00      7/25/2015 12:00:00 AM
1050001010      10000.00      7/25/2015 12:00:00 AM
My SQL View returns the three different records as expected. In the view I have both primary keys from both tables I have join in the sql view. The code just calls the view, I did not import the view into VS 2019.

Here is a log output that the original programmer captures.
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)

Any help would be greatly appreciated. Thanks
ASKER CERTIFIED SOLUTION
Avatar of Joe Pelish
Joe Pelish
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial