OVERLOAD VIEWS IN MVC?

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
 2290  View(s)
Ratings:
Rate this:

Hi Everyone!

I want to have links http://localhost:2409/Account/Confirmation/16 and that link http://localhost:2409/Account/Confirmation/ (without parametr).
But with this action methods, it isn't working. Why?

    public ActionResult Confirmation(int id, string hash)
    {
         Some code..

        return View();
    }
second, I just want to return View, if parametr is empty.

    public ActionResult Confirmation()
    {

        return View();
    }
Error (translated):

The current request for action on a controller Confirmation AccountController is ambiguous between the following methods of action:

System.Web.Mvc.ActionResult Confirmation (Int32, System.String) for type TC.Controllers.AccountController System.Web.Mvc.ActionResult Confirmation () for

type TC.Controllers.AccountController

Thanks in advance!

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: overload views in MVC?

    Hi Jayden!

     You can not create same action name with same http verb(e.g. HttpGet).

     You can overload view as following

     [HttpGet]
            public ActionResult Confirmation(int? id, string hash)
            {
                if (id.HasValue)
                {
                // put here some code
                    return View();
                }

                return View();
            }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!