-1

Every time I click on details for an item it gives me the error and the id is equal to 0, but if change the id number to anything greater than 1 within the number of items I have it runs properly Here is my method

public PhoneModel GetPhone(int id)
{
    using(var context = new ElectronicsDataBaseEntities())
    {
         var result = context.Phone
              .Where(x => x.Id == id)
              .Select(x => new PhoneModel()
              {
                  Name = x.Name,
                  Phonemodel = x.Model,
                  DateReleased = x.DateReleased,
                  Desicription = x.Desicription
               })
               .FirstOrDefault();

                return result;
      }
}

and this my action method in the controller

public ActionResult Details(int id) 
{
    var result = repository.GetPhone(id);
    return View(result);
}

Index The error

idex code index

adam p
  • 1
  • 2

1 Answers1

0

Add Id to your query, and check if your query has find something. Since your url shows that Id=0, you need to fix your index view. It must be the same story. You will have to add Id to select query for the index view.

.....
.Select(x => new PhoneModel()
                    {
                        Id=x.Id,

                        Name = x.Name,
                        Phonemodel = x.Model,
                        DateReleased = x.DateReleased,
                        Desicription = x.Desicription
                    }).FirstOrDefault();
....

Update

Try to use this code for your edit action link

@Html.ActionLink("Edit", "Edit","Phone", new { id=item.Id }) 

and fix etting all records method

public List<PhoneModel> GetAllPhones()
{  

 using(var context = new ElectronicsDataBaseEntities()) 
 { 
   var result = context.Phone .Select(x => new PhoneModel() 
  { 
   Id=x.Id,

  Name = x.Name, 
  Phonemodel = x.Model, 
  DateReleased = x.DateReleased, 
  Desicription = x.Desicription 
  }).ToList(); 

 return result; 
 } 
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • #Thanks. I have added the Id=x.Id but it's still coming up with that, am not really sure what you meant I must fix my index, but I deleted it and redid but it's still the same – adam p Jun 26 '21 at 15:51
  • Can you post your index view. pls. Index table link is not working properly. It maybe the same story. – Serge Jun 26 '21 at 15:55
  • they have now closed the question so I don't think you will be able to see the index code that I added. but here my links from the index @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) – adam p Jun 26 '21 at 16:27
  • @adamp You have to post query is used for Index model – Serge Jun 26 '21 at 17:18
  • Getting all records method public List GetAllPhones() { using(var context = new ElectronicsDataBaseEntities()) { var result = context.Phone .Select(x => new PhoneModel() { Name = x.Name, Phonemodel = x.Model, DateReleased = x.DateReleased, Desicription = x.Desicription }).ToList(); return result; } } – adam p Jun 27 '21 at 05:55
  • action method public ActionResult Index() { var result = repository.GetAllPhones(); return View(result); } – adam p Jun 27 '21 at 05:56