1
votes

Newbie in C# This is my code. and during debugging i get to this method with out error! but when it send the request back i get back the error listed above! so any ideas how to solve this? thanks

[HttpPost]
public ActionResult Delete(int auctionId, int productId) 
{
    Auction auct = auctionRepository.Auctions.FirstOrDefault(a => a.AuctionID == auctionId);

    if (auct != null) {
        auctionRepository.DeleteAuction(auct);
        TempData["message"] = string.Format(auct.AuctionID + " was deleted");
    }

    return RedirectToAction("Edit", "Admin", new { productId });
}

/////////

error =>

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 6: }

Line 7:

Line 8: <h2>Edit @Model.Title</h2>

Line 9: @using (Html.BeginForm("Edit", "Admin",FormMethod.Post, new { enctype = "multipart/form-data" })) {

Line 10: @Html.EditorForModel()

1

1 Answers

1
votes

It appears that when you remove an auction entry, it also removes your product, so when you do a re-direct to a page for editing that product, the product no longer exists and you get a null reference exception. Can you please check the database to make sure that your product still exists after you remove an auction? If that's not the case, then can you please post action method for editing your product?

Edit

If your product gets deleted, then you need to specify your cascade action. Can you please post your ER digram for products and auctions?