2012-05-11 69 views
7

Tôi gặp sự cố khi sử dụng phương thức RedirectToAction trong mvc. Tuyến đường Bản đồ của tôi trông như thế này.Cách sử dụng RedirectToAction đúng

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

Và sự trở lại giống như thế này:

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

Nó hoạt động hảo, nhưng tôi sử dụng các RedirectToAction nó vẫn trông giống như

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

điều khiển này:

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

    } 

Đang nhận bộ điều khiển:

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

Nhưng cách tôi muốn nó là như thế này.

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

Nhưng điều kỳ lạ là tôi có thể nhập thủ công trong Url khi nó hoạt động hoàn hảo. Tôi đang làm gì sai?

Trả lời

4

Tôi có một vài mẹo.

Trước tiên, đừng đặt tên cho tuyến đường của bạn. Truyền null cho đối số đầu tiên đó. Có một số quá tải RedirectToRoute có tên tuyến, và RedirectToAction trả về một RedirectToRouteResult.

Thứ hai, không cung cấp mặc định cho các thông số bắt buộc của bạn. Nếu bạn muốn có mặc định, hãy chỉ đặt giá trị mặc định cho thông số cuối cùng.

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

Thứ ba, khi nghi ngờ, hãy thử trả lại RedirectToRoute thay thế. Vượt qua bộ điều khiển, hành động và khu vực (nếu có) cùng với các thông số khác của bạn:

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

Mẹo cuối cùng là sử dụng T4MVC để loại bỏ các chuỗi ma thuật đó.

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

Bạn có thể sử dụng những loại mạnh tương tự trong RedirectToRoute, hoặc bạn có thể sử dụng partials T4MVC cho trở về hành động:

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

Xin chào. Đối với một số lý do tôi vẫn không thể làm cho nó hoạt động chính xác. Tôi đã thêm Bộ điều khiển gửi dữ liệu – mortenstarck

+1

Tôi vừa làm việc đó. Vấn đề là một vài nơi. 1. Trong tệp Global.asax.cs tôi cần phải cụ thể như thế này "Project/{action}/{machineName}/{projectId}/{directiveId}/{directiveName}" '. Và tôi đã sử dụng đề xuất của bạn bằng cách sử dụng RedirectToRoute thay vì ToAction và sau đó tôi đã làm việc. – mortenstarck

0

Khi tôi kiểm tra tuyến đường của bạn và RedirectToAction(), nó hoạt động chính xác khi tôi sửa trường hợp của thuộc tính "CompanyId" trong đối tượng ẩn danh thành "companyId".

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

Cập nhật: Đây là mã trong global.asax, Controllers, và View.

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

im làm sai là gì. Tôi vẫn không thể làm đúng. Nhưng nếu tôi gõ url đúng nó cũng làm việc – mortenstarck

Các vấn đề liên quan