Click here to Skip to main content
15,569,774 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.
         "Project",  // Route name
          new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 }  // Parameter defaults);

And the sending Controller look like this:
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 });
        ModelState.AddModelError("", "Invalid username or password");

    return View();


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]);

				ModelState.AddModelError("", "An error has ");

			ViewBag.DirectivesList = convertedDirectives;
			return View();

In the Adress bar it looks like this:

But the way i want it is like this.

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

1 solution

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

routes.MapRoute("Project",  // Route name
                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.

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
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