Mam problem z wykorzystaniem metody RedirectToAction w mvc. Trasa Moja Mapa wygląda następująco.Sposób korzystania z usługi RedirectToAction poprawny
routes.MapRoute(
"Project", // Route name
"{Controller}/{Action}/{projectId}/{machineName}/{companyId}",
new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 } // Parameter defaults);
i powrót wyglądać następująco:
return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id });
Działa prefektem, ale używam RedirectToAction nadal wygląda jak ten
http://localhost:1843/Project/Directives?projectId=48&machineName=Netduino&companyId=27
Kontroler:
[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();
}
Odbiorca kontroler:
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();
}
Ale sposób, w jaki chcę, jest taki.
http://localhost:1843/Project/Directives/48/Netduino/27
Ale dziwne jest to, że mogę ręcznie wpisać adres URL i działa prefekt. Co robię źle?
Hello. Z jakiegoś powodu nadal nie mogę sprawić, by działał poprawnie. Dodałem mój kontroler, który wysyła dane – mortenstarck
Właśnie dostałem to do pracy. Problem polegał na kilku miejscach. 1. W pliku Global.asax.cs potrzebuję czegoś takiego jak ten '' Project/{action}/{machineName}/{projectId}/{directiveId}/{directiveName} "'. I użyłem twojej sugestii użycia RedirectToRoute zamiast ToAction, a potem pracowałem. – mortenstarck