I have a default route specified like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "", RouteName = "Default" }, // Parameter defaults
new { controller = @"[^\.]*" } // Constraints (Ignore urls with periods in them)
);
I have a controller called Test and an action on Test called DoSomething that is defined like this:
public ActionResult DoSomething(int someId, int someotherId, IEnumerable<string> listOfSomething, bool flag, bool flag2)
I am trying to call the action like this:
var parameters = new RouteValueDictionary();
parameters.Add("someId", id);
parameters.Add("someotherId", otherId);
parameters.Add("flag", flag1);
parameters.Add("flag2", flag2);
for (int i = 0; i < thisList.Count; i++)
{
parameters.Add("listOfSomething[" + i + "]", thisList[i]);
}
Html.RenderAction("DoSomething", "Test", parameters);
The Html.RenderAction call is failing with the InvalidOperationException : No route in the route table matches the supplied values.
What would be causing this? The default route should pick this call up?
No route in the route table matches the supplied values indicates that no valid route in RouteCollection
matches the requested URL, ie your default route parameters count doesn't match with DoSomething
action parameters (1 parameter : 5 parameters).
Also note that IEnumerable<string>
considered as complex object, therefore you can't pass it as part of RouteValueDictionary
parameters in URL. Hence, you should pass only 4 value parameters & pass IEnumerable<string>
object as Session
or TempData
content.
First, define a custom route with 4 parameters as such on top of default one (avoid modifying default route which placed last in route order, it may required for other routes):
routes.MapRoute(
"Custom", // Route name
"{controller}/{action}/{someId}/{someotherId}/{flag}/{flag2}", // URL with parameters
new { controller = "Home", action = "Index", someId = "", someotherId = "", flag = "", flag2 = "" }, // Parameter defaults
new { controller = @"[^\.]*" } // Constraints (Ignore urls with periods in them)
);
Then, edit controller action method to receive 4 parameters (strip off IEnumerable<string>
from parameters list, based from explanation above):
public class TestController : Controller
{
public ActionResult DoSomething(int someId, int someotherId, bool flag, bool flag2)
{
// other stuff
}
}
And pass parameters for redirect thereafter, with IEnumerable
object stored in TempData
or Session
variable:
var parameters = new RouteValueDictionary();
parameters.Add("someId", id);
parameters.Add("someotherId", otherId);
parameters.Add("flag", flag1);
parameters.Add("flag2", flag2);
var list = new List<string>();
for (int i = 0; i < thisList.Count; i++)
{
list.Add(thisList[i]);
}
TempData["listOfSomething"] = list;
Html.RenderAction("DoSomething", "Test", parameters);
Or define parameters directly in RedirectToAction
:
var list = new List<string>();
for (int i = 0; i < thisList.Count; i++)
{
list.Add(thisList[i]);
}
TempData["listOfSomething"] = list;
Html.RenderAction("DoSomething", "Test", new { someId = id, someotherId = otherId, flag = flag1, flag2 = flag2 });
If thisList
is already IEnumerable<string>
instead, remove for
loop & just assign it straight to Session
/ TempData
:
TempData["listOfSomething"] = thislist;
The list of parameters can be retrieved using this way:
var listOfParameters = TempData["listOfSomething"] as List<string>;
Similar issues as reference:
How to pass List in Redirecttoaction
Sending a list using RedirectToAction in MVC4
Passing an array or list of strings from one action to another when redirecting
Routing with Multiple Parameters using ASP.NET MVC
How to pass multiple objects using RedirectToAction() in Asp.NET MVC?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.