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
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.
Overloading is the creation of more than one procedure, instance constructor, or property in a class with the same name but different argument types.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With