2014-09-12 40 views
1

我在我的應用程序有一個LogInOrRegister頁,調用2個兒童行動 LogInOrRegister.cshtmlMVC4 PartialViewResult返回一個視圖,而不是PartialView

@{ 
    ViewBag.Title = "Log in"; 
} 

@Html.Action("Login", "Account", new { returlUrl = ViewBag.ReturnUrl}) 
@Html.Action("Register", new { returlUrl = ViewBag.ReturnUrl}) 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

的登錄PartialView是:

@model Com.WTS.Portal.Models.LoginModel 
<hgroup class="title"> 
    <h1>@ViewBag.Title</h1> 
</hgroup> 

<section id="loginForm"> 
<h2>Use a local account to log in.</h2> 
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
    <legend>Log in Form</legend> 
    <ol> 
     <li> 
      @Html.LabelFor(m => m.Email) 
      @Html.TextBoxFor(m => m.Email) 
      @Html.ValidationMessageFor(m => m.Email) 
     </li> 
     <li> 
      @Html.LabelFor(m => m.Password) 
      @Html.PasswordFor(m => m.Password) 
      @Html.ValidationMessageFor(m => m.Password) 
     </li> 
     <li> 
      @Html.CheckBoxFor(m => m.RememberMe) 
      @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" }) 
     </li> 
    </ol> 
    <input type="submit" value="Log in" /> 
    </fieldset> 
} 
</section> 

我AccountController.cs包含以下代碼:

[AllowAnonymous] 
    public PartialViewResult Login(string returnUrl) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return PartialView(); 
    } 

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public PartialViewResult Login(LoginModel model, string returnUrl) 
    { 
     if (ModelState.IsValid && WebSecurity.Login(model.Email, model.Password, persistCookie: model.RememberMe)) 
     { 
      RedirectToLocal(returnUrl); 
     } 

     // If we got this far, something failed, redisplay form 
     ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     return PartialView(model); 
    } 

I看到正確的2局部視圖我GET頁面LogInOrRegister.cshtml

當我提交表單時,如果在表單中存在驗證錯誤,則顯示視圖(無佈局)而不是部分視圖應該是部分的LogInOrRegster

任何想法?

回答

1

好吧,我找到了解決方案。通過將路徑參數傳遞給局部視圖表單:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) 

我認爲我們改變了子動作的行爲。僅刪除路線屬性:

@using (Html.BeginForm()) 

PartialView呈現在其容器中。此外,我們可以定義POST行動ChildActionOnly,它仍然可以工作:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
[ChildActionOnly] 
public PartialViewResult Login(LoginModel model, string returnUrl) 
+0

如果任何修復它,我不知道如何。除了添加'ChildActionOnly'可以防止這個特定的'Login'操作被調用。你有另外一個實際返回'ViewResult'的'Login'操作嗎?總而言之,如果您的操作返回「PartialViewResult」,那麼您將獲得的是部分視圖,即無佈局。獲得佈局的唯一方法是返回'ViewResult'。 – 2014-09-12 18:18:52

+0

另外,請記住,您不能發佈到兒童行動。 – 2014-09-12 18:20:47

1

所以如果你看看在這裏找到的討論https://stackoverflow.com/a/10253786/30850你會看到ChildActionOnlyAttribute被使用,以便一個Action可以在視圖內呈現,但不能被提供給瀏覽器。

+0

你知道爲什麼當我移除ChildActionOnly屬性時,當調用返回的PartialView()時,我得到了沒有佈局的局部視圖? – 2014-09-12 16:32:42

+0

我會更新我的問題,然後... – 2014-09-12 16:37:39

+0

部分視圖應該返回一個沒有佈局的局部視圖。如果你想要的佈局,你只需返回一個視圖 – 2014-09-16 23:06:41

0

如果返回類型爲PartialViewResult,在方法public PartialViewResult Login(LoginModel model, string returnUrl)與模型一起指定以其它方式使用ActionResult作爲返回類型partialview名。 ActionResult是一個抽象類,其中PartialViewResult是一個子類。

相關問題