6

我需要在主頁上顯示註冊表單和登錄表單。ASP .Net MVC 3:子操作和重定向

如果驗證在這兩個表單上失敗,我需要在主頁上顯示適當的錯誤。

但是,如果沒有錯誤,用戶必須重定向到受保護的儀表板。

要做到這一點,我在主頁上使用child action這樣的:

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

它完美地工作,如果有任何錯誤的預期,因爲它是能夠re-renderpartial view用適當的model那有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

您不應該在該上下文中使用子操作。

您的問題的解決方案可能是 在您的頁面中放置兩個表單,一個用於註冊,另一個用於登錄。註冊表在會員控制器中發佈到註冊操作,登錄操作發佈到成員控制器中的登錄操作。

在情況下,您可以採取的行動之一發生錯誤:

  • 顯示專用登錄/註冊頁面和使用模式/驗證結果顯示錯誤消息
  • 重定向到URL /行動用戶來自何處,並顯示你在TempData的將錯誤消息要做到這一點
1

不使用的JavaScript重定向: 如果你把表格放在你的裏面r子視圖,有時如果在Beginform助手(子視圖內)中指定操作名稱和控制器名稱,則不會發生此問題。比如我改變了我的孩子動作看法是這樣的:

前:

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

後:

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

現在,你可以把RedirectAction命令裏面的 「InsertComment」 行動,一切都將正常工作。


兩種形式,一個頁面管理:

1.Specify名稱提交按鈕(每個表單)(例: 「submitvalue」)

Form1中:

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

form2:

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

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是我們的目標。