0

I have a detail pages which has the ids, but if i don't pass id in http://mysite.com/Detail?id=131 or if I just type http://mysite.com/Detail i get the error

Cannot perform runtime binding on a null reference

i want to show custom error message on this, how to do this ?

4

1 に答える 1

1

You may make id parameter nullable and check it in action like this:

public ActionResult Detail(int? id)
{
  if (id.HasValue() == false)
  { return custom error message }
}

Or you may annotate your nullable id with [Required(ErrorMessage = "error message")] to get client validation as well. And perform server validation like:

public ActionResult Detail(int? id)
{
  if (ModelState.IsValid == false)
  { return custom error message }
}
于 2012-09-20T08:46:41.020 に答える