2012-05-11 5 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); 

とリターンは次のようになります。それは知事に動作しますが、私はそれはまだこの

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

コントローラーのように見えますRedirectToAction使用

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

 [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 

しかし、奇妙なことは私が手動で入力できることです。何が間違っているのですか?

答えて

4

私はいくつかのヒントを持っています。

まず、ルートの名前を付けないでください。最初の引数にnullを渡します。ルート名を取るいくつかのRedirectToRouteオーバーロードがあり、RedirectToAoutはRedirectToRouteResultを返します。

第2に、必要なパラメータにデフォルトを設定しないでください。デフォルトを使用する場合は、最後のパラメータだけをデフォルトにします。

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

第3の疑問がある場合は、代わりに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}"のように具体的に記述する必要があります。そして私はToActionの代わりにRedirectToRouteを使用するというあなたの提案を使用し、その後私は働いていました。 – 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

関連する問題