2016-05-02 15 views
0

HTTPPostアクションを使用して別のコントローラにリンクしようとしていますが、現在のページのコントローラにルート値を追加しようとしました。たとえば、Site/ViewIndexからPage/createPageにフォームとHTTPPOSTをリンクしようとすると、404がスローされ、Site/Page/createPageにアクセスできないと表示されます。なぜこれをやっているのですか?それをどうやって止めることができますか?ここでアクションルーティングが同じコントローラに移動する

は、私のサイト/のcreatePageです:

<form method="post" action="Page/createPage"> 
    <input class="form-field form-control" type="text" name="title" placeholder="Page Title" /> 
    <input class="form-field form-control" type="text" name="description" placeholder="Page Description" /> 
    <input class="form-field form-control" type="hidden" name="siteId" value="@site.Id" /> 
    Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" /><br /><br /> 
    <input class="btn btn-info" type="submit" value="Create" /> 
</form> 

そして、私はそれがどんな関連性だが、ここに私のサイトコントローラは疑う:ここ

public ActionResult createPage(int siteId, string title, string description, bool isBlog = false) 
     { 
      if (string.IsNullOrWhiteSpace(title) || 
       string.IsNullOrWhiteSpace(description)) 
      { 
       return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Please fill out all fields" }); 
      } 
      try 
      { 
       Ops.PageOps.createPage(title, description, siteId, isBlog); 
       return RedirectToAction("ViewIndex", "Site", new { siteId = siteId, message = "Page created!" }); 
      } 
      catch (Exception e) 
      { 
       return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Error occured: " + e.Message }); 
      } 
     } 

は私のフォームです

public class SiteController : Controller 
    { 
     /// <summary> 
     /// The create page 
     /// </summary> 
     /// <returns></returns> 
     public ActionResult CreateIndex() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Create(string title, string description, bool privateSite = false) 
     { 
      Ops.SiteOps.createSite(Authenticated.AuthenticatedAs, title, description, privateSite); 

      return RedirectToAction("Index", "Home"); 
     } 

     public ActionResult ViewIndex(int siteId, string message = null) 
     { 
      ViewBag.message = message; 
      ViewBag.siteId = siteId; 
      return View(); 
     } 


    } 
+0

どのようにリンクしていますか?コードを表示できますか? – Shyju

+0

@Shyju更新された質問をご覧ください – RhysO

答えて

1

使用フォームタグをレンダリングするヘルパーメソッドHtml.BeginFormこれにより、フォームのアクション属性のHttpPostアクションに対する正しい相対パスがレンダリングされます。

@using(Html.BeginForm("CreatePage","Page")) 
{ 
    <input class="form-field form-control" type="text" name="title" placeholder="Title" /> 
    <input class="form-field form-control" type="text" name="description" " /> 
    <input class="form-field form-control" type="hidden" name="siteId" value="@site.Id" /> 
    Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" /> 
    <input class="btn btn-info" type="submit" value="Create" /> 
} 
関連する問題