简体   繁体   English

MVC4 PartialViewResult返回视图而不是PartialView

[英]MVC4 PartialViewResult return a view and not a PartialView

I have an LogInOrRegister page in my application, that calls 2 child actions LogInOrRegister.cshtml 我的应用程序中有一个LogInOrRegister页面,它调用2个子操作LogInOrRegister.cshtml

@{
    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")
}

The Login PartialView is: Login 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>

My AccountController.cs contains the following code: 我的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 see correctly the 2 partial view the I GET the page LogInOrRegister.cshtml 我正确地看到了我获取页面LogInOrRegister.cshtml的2个局部视图

When I submit the form, if there is validation errors in the form, the view is displayed (no layout) instead of the partial view that should be part of the LogInOrRegster 当我提交表单时,如果表单中存在验证错误,则显示视图(无布局),而不是应该成为LogInOrRegster一部分的局部视图

Any idea ? 任何想法 ?

Ok, I found te solution. 好的,我找到了te解决方案。 By passing route parameters to the partial view form : 通过将路径参数传递给局部视图表单:

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

I think we change the behaviour of the child action. 我认为我们改变了儿童行为的行为。 Only by removing the route attribute : 只有删除路由属性:

@using (Html.BeginForm())

The PartialView is rendered in its container. PartialView在其容器中呈现。 Also, we can define the POST action as ChildActionOnly, it still work: 此外,我们可以将POST操作定义为ChildActionOnly,它仍然可以工作:

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

因此,如果您查看https://stackoverflow.com/a/10253786/30850中的讨论,您将看到使用了ChildActionOnlyAttribute,以便可以在视图内呈现Action,但无法将其提供给浏览器。

If the return type is PartialViewResult ,specify the partialview name along with model in the method public PartialViewResult Login(LoginModel model, string returnUrl) otherwise use ActionResult as return type. 如果返回类型是PartialViewResult ,请在方法public PartialViewResult Login(LoginModel model, string returnUrl)指定partialview名称和model,否则使用ActionResult作为返回类型。 ActionResult is an abstract class where as PartialViewResult is a subclass. ActionResult是一个抽象类,其中PartialViewResult是一个子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM