How do I set up routes to enable me to call different actions on the same controller?
Posted
by Remnant
on Stack Overflow
See other posts from Stack Overflow
or by Remnant
Published on 2010-05-09T13:43:10Z
Indexed on
2010/05/09
13:48 UTC
Read the original article
Hit count: 287
asp.net-mvc
|routing
I am building my first asp.net mvc application for learning and development purposes and have come across an issue that I'd like some guidance with.
Suppose I have a controller with two actions as follows:
public class MyController : Controller
{
public ActionResult Index()
{
dbData = GetData("DefaultParameter")
return View(dbData);
}
public ActionResult UpdateView(string dbParameter)
{
dbData = GetData("dbParameter");
return View(dbData);
}
}
On my webpage I have the following:
<% using (Html.BeginForm("UpdateView", "MyController")) %>
<% { %>
<div class="dropdown"><%=Html.DropDownList("Selection", Model.List, new { onchange="this.form.submit();" })%></div>
<% } %>
I have the following route in Global.asax
:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Default", "{controller}", new { controller = "MyController", action = "Index"});
}
The issue I am having is as follows:
When I use the dropdownlist I get an error saying that /MyController/UpdateView
could not be found. It therefore seems that I need to add an additional route as follows:
routes.MapRoute("Default", "{controller}", new { controller = "MyController", action = "UpdateView"});
However, this causes two new issues for me:
- Due to the hierachy within the routing list, the similarity of the routes means that the one that appears first in the list is always executed.
- I don't see why I need to create another route for
UpdateView.
All I want to do is to retrieve new data from the database and update the view. I don't see what this has to do with the URL schema.
It feels like I have gone down the wrong track here and I have missed something quite fundamental?
© Stack Overflow or respective owner