6

ホームページに登録フォームとログインフォームを表示する必要があります。ASP .Net MVC 3:チャイルドアクションとリダイレクト

これらの2つのフォームで検証が失敗した場合は、ホームページに適切なエラーを表示する必要があります。

エラーがない場合、ユーザーは保護されたダッシュボードにリダイレクトする必要があります。

、これを達成するために、私はこのようなホームページにchild actionを使用しています:

@Html.Action("Register", "Membership") 

それはre-render適切modelpartial viewすることができますとしてそれは、エラーがある場合に予想されるとして完璧に動作していますvalidation stateという情報があります。

しかし、エラーがなかった場合、それはリダイレクトしようとすると、それはそれを示すエラーがスローされます。

Child actions are not allowed to perform redirect actions. 

は、この周りに方法はありますか?私は、ホームページに登録フォームとログインフォームを入れる方法があると確信しています。ほとんどの場合、私はASP .NET MVCの初心者なので分かりません。

ここで私を正しい方向に向けることができますか?

答えて

6

これを行うには、ログインと登録ビットにajaxフォームを使用し、送信が有効なときにRedirectResultを返す代わりに、 jsonのクライアント側のスクリプトはリダイレクトを監視し、それを使ってあなたのためにリダイレクトを行います。

ここでは簡単な例を示します。

コントローラー:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.ComponentModel.DataAnnotations; 
using MvcApplication12.Models; 

namespace MvcApplication12.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     public ActionResult Register() 
     { 
      return PartialView(new UserDetails()); 
     } 

     [HttpPost] 
     public ActionResult Register(UserDetails details) 
     { 
      if (ModelState.IsValid) 
      { 
       return Json(new {redirect = true, url = Url.Action("Index","Dashboard")}); 
      } 
      else 
      { 
       return PartialView(details); 
      } 
     } 
    } 
} 

ホームページ 'インデックス' ビュー:

@{ 
    ViewBag.Title = "Index"; 
} 
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
    function checkForRedirect(data) 
    { 
     if (data.redirect && data.url) 
     { 
      location.href = data.url; 
     } 
    } 
</script> 

<p>Home page stuff.....</p> 

<div id="RegistrationArea"> 
    @Html.Action("Register") 
</div> 

<p> Home page stuff.....</p> 

登録フォームの登録 "部分ビュー:

@model MvcApplication12.Models.UserDetails 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Ajax.BeginForm(new AjaxOptions() 
{ 
    HttpMethod = "POST", 
    Url = Url.Action("Register", "Home"), 
    OnSuccess = "checkForRedirect(data)", 
    UpdateTargetId = "RegistrationArea", 
    InsertionMode = InsertionMode.Replace 
})) 
{ 
    @Html.LabelFor(m => m.UserName) 
    @Html.EditorFor(m => m.UserName) 
    @Html.ValidationMessageFor(m => m.UserName) 

    @Html.LabelFor(m => m.Password) 
    @Html.EditorFor(m => m.Password) 
    @Html.ValidationMessageFor(m => m.Password) 

    <input type="submit" /> 
} 
2

このような状況では、子アクションは使用しないでください。

あなたの問題への解決策は、登録用とログイン用の2つのフォームをページに配置することです。登録フォームは、メンバーシップコントローラーの登録アクションにポストします。ログインアクションは、メンバーシップコントローラーのログインアクションにポストします。エラーはあなたができるアクションのいずれかで発生した場合には

  • がURLにエラーメッセージに
  • リダイレクトを表示するために、専用のログイン/登録ページおよび使用モデル/検証結果を表示/アクションユーザーが来て、TempDataに配置するエラーメッセージを表示する
1

リダイレクトのためにJavaScriptを使用しない場合: フォームをあなたの中に入れるとr子ビュー、Beginformヘルパー(子ビュー内)でアクション名とコントローラー名を指定すると、この問題は発生しないことがあります。後

@using (Html.BeginForm()) 
{ 
... 
} 

::前

:例えば私はこのような私の子供の行動ビューを変更

@using (Html.BeginForm("InsertComment", "Comments", FormMethod.Post, new { id = "commentform" })) 
{ 
... 
} 

、あなたは "InsertComment" アクション内RedirectActionコマンドを置くことができ、すべてが動作します。


つ1つのページの管理でフォーム:

をForm1:

<input type="submit" value="login" name="submitValue" class="btn btn-success pull-right" /> 
:送信ボタン(各フォーム)( "submitvalue" EX)用

1.Specify名

form2:

<input type="submit" value="register" name="submitValue" class="btn btn-success pull-right" /> 

2.これらのフォームに対して2つのアクションを実行します。 (例:「登録」と「ログイン」)

[HttpPost] 
    public ActionResult Login(LoginVM model, string submitValue) 
      { 
       if (submitValue == "login") 
       { 
       //Do Something 
       } 
       ... 
      } 


[HttpPost] 
public ActionResult Register(RegisterVM model, string submitValue) 
      { 
       if (submitValue == "register") 
       { 
       //Do Something 
       } 
       ... 
      } 
  • あなたがフォーム内のレジスタまたはログインボタンをクリックすると、アクションの両方が呼ばれているが、我々はwhichoneを決定文は、私たちの目標である「場合」と。