2

Basically put, I have a MVC project and I just have the default route.

(Question edited as after testing and re-reading my question, I am sure the error is just because ints can not be null)

I have read complex answers on here about overloading, but if I want to have two different results for a page call, say one if it is null and the other if it is passed a request, is the following sufficient?

    public ActionResult Details(int? id)
    {
        if(id != null)
            id=1;
        return View(id);
    }

and then can this be extended to more complex situations such as public ActionResult Details(int? id, string bla, string bla2) and just have a combination of null checks to return different views, or am I best implementing a solution such as on this answer.

Community
  • 1
  • 1
Wil
  • 10,234
  • 12
  • 54
  • 81
  • Sorry, again, title sucks but wasn't sure on a better one... feel free to change. – Wil Apr 30 '11 at 12:36

1 Answers1

3

While the last parameter is optional, the mapping engine doesn't find an Action for the route without the id parameter that fits - thus the error. You have two possibilites - either the way you described, or by making a default action:

public ActionResult Details ()
{
  return View(); //default view for this controller, could be a redirect as well
}

Keep in mind though, this won't work if your id is something nullable (in the case of a string for example, anything Nullable<T>), as the mapping engine will find ambigious overloads. In that case, there is no way around the null check.

Femaref
  • 60,705
  • 7
  • 138
  • 176