2013-12-10 15 views
9

こんにちはリダイレクトされないRedirectToActionに問題があります。私のコードは、リダイレクトに置かれたブレークポイントにヒットするので、呼び出されていることを確認できます。しかし、何も起こっていないようです。MVC 5 - リダイレクトされないリダイレクト

私はChromeのネットワークトラフィックを調べてみましたが、何も明らかではないようです。私は何かをシンプルに欠いているに違いない

// 
    // POST: /Blog/CreateBlog 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult CreateBlog(BlogViewModel model) 
    { 
     var userId = User.Identity.GetUserId(); 
     model.UserId = userId; 

     if (ModelState.IsValid && model.UserId != null) 
     { 
      Mapper.CreateMap<BlogViewModel, Blog>(); 
      if (_blogProcess.CreateBlog(Mapper.Map<BlogViewModel, Blog>(model))) 
      { 
       RedirectToAction("Index", "Blog"); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

答えて

29

Nalaka526の答えにaddtionで

return RedirectToAction("Index", "Blog"); 
+0

Jeebus ...その小さなキーワードを見落とすことは、顔に深刻な打撲です。私たちの正気をチェックしていただきありがとうございます! – CodeBreaker

3

を試してみてください。私たちはRedirectToActionためdocumentationに見れば、我々はそれが派生し、戻り値の型としてRedirectToRouteResultを持ってControllerクラスのインスタンスメソッドだと見ることができますActionResultからはreturnにする必要があります。たとえば、ViewPartialViewのように返します。

0
  1. あなたは

    その後、ビューページに

    public ActionResult CreateBlog(BlogViewModel model) 
    { 
        .... 
        return RedirectToAction("Index", "Blog") 
    } 
    
    を@using(Html.BeginForm( "インデックス"、.. "ブログ")を使用している場合は動作するはずです。

  2. あなたの場合Ajax.BeginFormを使用している、あなたはあなたのビューのためのjavascript OnSuccessイベント。 サンプルコードから新しいアクション/ URLにリダイレクトする必要が

    @using (Ajax.BeginForm("Index", "Blog", new AjaxOptions { HttpMethod = "post" , OnSuccess="RedirectFunction"} 
    
    function RedirectFunction(data) 
    { 
        window.location.href = “Index”; 
    } 
    

希望します。

関連する問題