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 return look like this:
return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id });
It works prefect, but the i use the RedirectToAction it still looks like this
http://localhost:1843/Project/Directives?projectId=48&machineName=Netduino&companyId=27
Controller:
[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();
}
The receiving 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();
}
But the way i want it is like this.
http://localhost:1843/Project/Directives/48/Netduino/27
But the weird thing is that i can manual type in the Url an it works prefect. What is im doing wrong?
I have a few tips.
First, don't name your routes. Pass null to that first argument. There are several RedirectToRoute overloads that take a route name, and RedirectToAction returns a RedirectToRouteResult.
Second, don't give defaults to your required parameters. If you want to have a default, give a default only to the last parameter.
routes.MapRoute(null, // don't name the route
"{controller}/{action}/{projectId}/{machineName}/{companyId}",
new
{
controller = "Project",
action = "Directives",
//projectId = 0,
//machineName = "",
//companyId = 0,
}
);
Third, when in doubt, try returning a RedirectToRoute instead. Pass the controller, action, and area (if applicable) along with your other parameters:
return RedirectToRoute(new
{
controller = "Project",
action = "Directives",
// area = "SomeAreaName", // if the action is in an area
projectId = projectId,
machineName = project.MachineName,
companyId = user.Company_Id,
});
Last tip is use T4MVC to get rid of those magic strings.
routes.MapRoute(null, // don't name the route
"{controller}/{action}/{projectId}/{machineName}/{companyId}",
new
{
controller = MVC.Project.Name,
action = MVC.Project.ActionNames.Directives,
}
);
You can use those same strong types in RedirectToRoute, or you can use the T4MVC partials for returning to actions:
return RedirectToAction(MVC.Project.Directives
(projectId, project.MachineName, user.Company_Id));
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