Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi mates,

I want to overload the default Index ActionResult method in ASP.NET MVC3 like:

C#
public ActionResult Index()
       {
           ViewBag.Message = "Hello from Parameter-Less Index Method.";
           return View();
       }


       public ActionResult Index(string myName)
       {
           ViewBag.Message = "Hello from Parameterized Index Method. <br/>Hello "+myName;
           return View();

       }


is it possible? If yes, the how? I appreciate any help !

Best regards,
Sunny K
Posted

1 solution

I guess overloading in not allowed for index method.

The temporary way is:

SQL
public ActionResult Index(string myName)
        {
            if (String.IsNullOrEmpty(myName))
            {
                ViewBag.Message = "Hello from Parameter-Less Index Method.";
                return View();
            }
            else
            {
                ViewBag.Message = "Hello from Parameterized Index Method. <br/>Hello " + myName;
                return View();
            }
        }
 
Share this answer
 
Comments
Sunny_Kumar_ 3-Nov-12 4:37am    
I really appreciate your effort, but I don't need this approach. Thanks anyway.
shrikant shukla2010 6-Oct-14 3:24am    
Nice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900