-3

what I've got is a edit page and a create page. That are two links in another page, I am trying to create a redirect when the user tries to press on the edit button, if there is no value in the id, the site will take the user to the add page.

Public ActionResult Edit (int id)
{
   if (Product.Id.Equals(null))
   {
      RedirectToAction("Add");
   }
//Edit Page Code is here
}

what I've got is wrong

jsg
  • 1,224
  • 7
  • 22
  • 44
  • You're "guessing what you've got is wrong." Have you not tried to run this? You should probably be checking for zero instead of `null` – eddie_cat Oct 01 '14 at 14:19
  • 1
    I doubt if that is a good way to check for `null`: `Product.Id.Equals(null)`, because either it is not null or you get an exception. You just have to use `==`: `if (Product.Id == null)` – Tim Schmelter Oct 01 '14 at 14:21

2 Answers2

2

You need to set the id as nullable, either "int? id" or "Nullable<int> id".

    Public ActionResult Edit (int? id)
    {
           if (!id.HasValue)
           {
             RedirectToAction("Add");
           }
           // Edit code goes here   

    }

`

Yusuf Demirag
  • 773
  • 7
  • 10
0

You can set the variable to nullable. Just put ? on your variable.

Public ActionResult Edit (int? id)
{
   if (Product.Id.Equals(id))
   {
     RedirectToAction("Add");
   }
}
jayvee
  • 160
  • 2
  • 17