Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an problem using the RedirectToAction method in mvc. My Map route look like this.
routes.MapRoute(
         "Project",  // Route name
         "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",
          new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 }  // Parameter defaults);

And the sending Controller look like this:
[HttpPost]
[ValidateInput(false)]
public ActionResult Create(Project project, string text)
{
    ViewBag.MachineType = new List<string>(new string[] { "Machine Type A", "Machine Type B", "Machine Type C", "Machine Type D", "Machine Type E" });

    if (ModelState.IsValid)
    {
        UserAccess user = (UserAccess)(Session["UserAccessInfo"]);

        Int64 projectId = DataBase.DBProject.InsertProject(project.ProjectNumber, project.MachineName, project.MachineNameEnglish, 1, project.Serial, text, user.Company_Id,project.MachineType);

        return RedirectToAction ("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 });
       // return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id });
    }
    else
    {
        ModelState.AddModelError("", "Invalid username or password");
    }

    return View();

}</pre>


And the recivering controller:
public ActionResult Directives(int projectId, string machineName, int companyId)
		{
			convertedDirectives = 
new List<Models.Directives>();
			ViewBag.MachineName = machineName;
			ViewBag.ProjectId = projectId;
			List<object> Directives = new List<object>();
		   
			if (ModelState.IsValid)
			{
				Directives = DataBase.DBProject.GetDirectives(companyId);
				foreach (List<object> dir in Directives)
				{
					Directives newDirective = new Models.Directives();
					newDirective.CompanyID = Convert.ToInt32(dir[0]);
					newDirective.DirectiveId = Convert.ToInt32(dir[1]);
					newDirective.Id = Convert.ToInt32(dir[2]);
					newDirective.DirectiveName = Convert.ToString(dir[3]);
					newDirective.DirectiveNameShort = Convert.ToString(dir[4]);
					newDirective.Created = Convert.ToDateTime(dir[5]);

					convertedDirectives.Add(newDirective);
				}
			}
			else
			{
				ModelState.AddModelError("", "An error has ");
			}

			ViewBag.DirectivesList = convertedDirectives;
			return View();
		}


In the Adress bar it looks like this:
http://localhost:1843/Project/Directives?projectId=48&machineName=NetDuino&companyId=27


But the way i want it is like this.
http://localhost:1843/Project/Directives/48/Netduino/27


But if trie to type the url in manuel it works prefect. What is im doing wrong?
Posted

1 solution

Try RedirectToRoute. That should work for your case. A quick example is given below:

routes.MapRoute("Project",  // Route name
                "Route/Directives/{projectId}/{machineName}/{companyId}",
                new { controller = "Route", action = "Directives", projectId = @"\d+", machineName = @"\S+", companyId = @"\d+" });


Notice that I have hard-coded the controller & action, you could also use your method and pass the controller and action as route values.

Now in your controller, instead of RedirectToAction, use RedirectToRoute as shown in the Index action method.

C#
public class RouteController : Controller
{
    //
    // GET: /Route/

    public ActionResult Index()
    {
        return RedirectToRoute("Project", new { projectId = 4, machineName = "somemachine", companyId = 7 });
    }

    public ActionResult Directives(int projectId, string machineName, int companyId)
    {
        ViewData["Data"] = string.Format("{0},{1},{2}", projectId, machineName, companyId);
        return View();
    }
}


Now, if you navigate to /route/index it will redirect you to /Route/Directives/4/somemachine/7 instead of how you get currently.

Hope this helps!
 
Share this answer
 
v3
Comments
mortenstarck 14-May-12 4:10am    
Hey thanks for it. It works almost. It displays correct. But it doesnt redirect me to /Project/Directives/.... but instead to Project/Create/../../.
Karthik. A 14-May-12 9:23am    
Are you sure you have the correct action name in the route defined in Global.asax ? If possible pls. update your question with the changes you have done, so that I could find out the mistake.

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