2012-05-11 272 views
7

我在使用mvc中的RedirectToAction方法时遇到问题。我的地图路线是这样的。如何使用RedirectToAction正确

   routes.MapRoute(
     "Project", // Route name 
     "{Controller}/{Action}/{projectId}/{machineName}/{companyId}", 
      new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 } // Parameter defaults); 

而且回这个样子:

return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id }); 

它的工作原理知府,但我用它仍然看起来像这样

http://localhost:1843/Project/Directives?projectId=48&machineName=Netduino&companyId=27 

控制器的RedirectToAction:

 [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(); 

    } 

接收控制器:

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(); 
    } 

但我想要的方式是这样的。

http://localhost:1843/Project/Directives/48/Netduino/27 

但奇怪的是,我可以在Url手动输入它是一个完美的工作。我做错了什么?

回答

4

我有一些提示。

首先,不要命名你的路线。将null传递给第一个参数。有几个RedirectToRoute重载接受路由名称,RedirectToAction返回一个RedirectToRouteResult。

其次,不要给你默认的参数。如果你想有一个默认的,只给最后一个参数默认。

routes.MapRoute(null, // don't name the route 
    "{controller}/{action}/{projectId}/{machineName}/{companyId}", 
    new 
    { 
     controller = "Project", 
     action = "Directives", 
     //projectId = 0, 
     //machineName = "", 
     //companyId = 0, 
    } 
); 

第三,如果有疑问,请尝试返回RedirectToRoute。通过控制器,动作和地区(如适用)和其他参数:

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, 
}); 

最后一个技巧是使用T4MVC摆脱那些神奇的字符串。

routes.MapRoute(null, // don't name the route 
    "{controller}/{action}/{projectId}/{machineName}/{companyId}", 
    new 
    { 
     controller = MVC.Project.Name, 
     action = MVC.Project.ActionNames.Directives, 
    } 
); 

可以在RedirectToRoute使用那些相同的强类型,或者您可以使用T4MVC谐音返回到行动:

return RedirectToAction(MVC.Project.Directives 
    (projectId, project.MachineName, user.Company_Id)); 
+0

你好。出于某种原因,我仍然无法让它正常工作。我已经添加了我的控制器,它发送数据 – mortenstarck

+1

我刚刚开始工作。问题出现在几个地方。 1.在Global.asax.cs文件中,我需要特定像这样的“Project/{action}/{machineName}/{projectId}/{directiveId}/{directiveName}”。我用你的建议使用RedirectToRoute而不是ToAction,然后我工作。 – mortenstarck

0

当我测试您的路由和RedirectToAction()时,它在我将匿名对象中的“CompanyId”属性更正为“companyId”的情况下正常工作。

return RedirectToAction("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 }); 

更新: 这里是在Global.asax中的代码中,控制器和视图。

routes.MapRoute(
      "Project", // Route name 
      "{Controller}/{Action}/{projectId}/{machineName}/{companyId}", 
       new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 } // Parameter defaults 
); 

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return RedirectToAction("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 }); 
    } 
} 

public class ProjectController : Controller 
{ 
    public ActionResult Directives(int projectId, string machineName, int companyId) 
    { 
     ViewBag.ProjectId = projectId; 
     ViewBag.MachineName = machineName; 
     ViewBag.CompanyId = companyId; 
     return View(); 
    } 
} 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Directives 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2> 
     Directives</h2> 
    <%: ViewBag.ProjectId %><br /> 
    <%: ViewBag.MachineName %><br /> 
    <%: ViewBag.CompanyId %><br /> 

    <%: this.Request.RawUrl %> 
</asp:Content> 
+0

什么是即时通讯做错了。我仍然无法做到。但是,如果我输入正确的URL也可以工作 – mortenstarck