Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC - Overload Action method

I created an asp.net mvc web site

My problem is how to implemented overload action method

Controller


    public ActionResult Index(int id)
    {
        //code
        return View(model);
    }

    public ActionResult Index()
    {
        //code  
        return View(model);
    }

View


    <div id="menucontainer">
            <ul id="menu">          
                    <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
                    <%if (Page.User.Identity.IsAuthenticated)
                      {%>
                    <li><%= Html.ActionLink("Profilo", "Index", "Account")%></li>
                    <%} %>
                    <li><%= Html.ActionLink("About", "About", "Home")%></li>
                </ul>
            </div>

Usercontrol (ascx) inserted in the View. This usercontrol lists the friends of the profile (view)


   <td>
            <%= Html.ActionLink(Html.Encode(item.Nominativo), "Index", "Account", new { id = item.IdAccount }, null)%>
        </td>

Global asax


    public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", 
                "{controller}/{action}/{id}", 
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );
        }

when i click the action Index in the view, return the error "Can not find the resource... ecc.."

I found several answer for this problem (using attribute ecc..) but is not works.

There's a way to do that? I must add a maproute in a global asax?

thanks so much for your replies

like image 931
Alberto Rubini Avatar asked Mar 15 '11 00:03

Alberto Rubini


People also ask

Can action method be overloaded in MVC?

If we have to overload the action Method in asp.net MVC then we can not do it directly. We have to change the ActionName like this code snippet. Now to run the controller GetEmpName action method with just give the URL like this: http://localhost:49389/Home/GetEmpName.

What is overloading asp net?

Overloading is the creation of more than one procedure, instance constructor, or property in a class with the same name but different argument types.

Can we have same action name in MVC?

While ASP.NET MVC will allow you to have two actions with the same name, . NET won't allow you to have two methods with the same signature - i.e. the same name and parameters. You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.

Can we overload method in web API?

Method overloading can be done in a web service with the following things: By changing the number of parameters used. By changing the order of parameters. By using different data types for the parameters.


Video Answer


1 Answers

You need to decorate both overloads with an ActionMethodSelector attribute for disambiguation. ASP.NET MVC does not know how to select the appropriate overload.

A workaround is to handle both actions in the same method:

public ActionResult Index(int? id) {

   if (id.HasValue) {
      // id present
   } else {
      // id not present
   }
}
like image 104
Max Toro Avatar answered Sep 28 '22 05:09

Max Toro