简体   繁体   English

子操作不允许执行重定向操作MVC4

[英]Child actions are not allowed to perform redirect actions MVC4

Im very new to MVC and messing around. 我对MVC还是很陌生。 This has been driving me crazy all day, i have been reading around and it seems that i may have some fundamental errors in the structure of my code. 这使我整日发疯,我一直在阅读,似乎我的代码结构中可能存在一些基本错误。 But i am struggling to see what they are exactly. 但是我正在努力查看它们的确切含义。 As far as i can see i am not even using a child action? 据我所知,我什至不使用儿童动作?

The error is being thrown on the return RedirectToAction line. 在返回的RedirectToAction行上引发了错误。

I have a registration form that i am displaying in a modal. 我有一个以表格形式显示的注册表。 This modal will be available to see on many pages - so i created an action that returns a partial view. 这种模式可以在许多页面上看到-因此我创建了一个返回部分视图的动作。

[AllowAnonymous]
public ActionResult RegisterPartial()
{
    return PartialView(new RegisterModel());
}



[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]

public ActionResult RegisterPartial(RegisterModel model)
{
    if (ModelState.IsValid)
    {

      //do stuff

return RedirectToAction("Data", "Settings", new { id = model.UserName });

    }

    // If we got this far, something failed, show form again
    return PartialView(model);
}

This is called from my home page at the moment - but will be called from many pages eventually. 目前,这是从我的主页上调用的-但最终会在许多页面上调用。

<div class="modal fade" id="signUpModal">
    @Html.Action("RegisterPartial", "Account")
</div>

<a href="#" style="color:#fff;" class=" btn btn-lg btn-primary" data-toggle="modal" data-target="#signUpModal">SignUp</a>

And here is the partial view itself 这是局部视图本身

@model Prog.Models.RegisterModel


<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title">Signup</h4>
        </div>

            @using (Html.BeginForm())
            {
                <fieldset>

                    <div class="modal-body">

                        <p class="message-info">
                        </p>
                        @Html.ValidationSummary()
                        @Html.AntiForgeryToken()
                        <ol>
                            <li>
                                @Html.LabelFor(m => m.UserName)
                                @Html.TextBoxFor(m => m.UserName, new { @maxlength = "13" })
                                @Html.ValidationMessageFor(m => m.UserName)
                            </li>
                            <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)
                            </li>
                            <li>
                                @Html.LabelFor(m => m.ConfirmPassword)
                                @Html.PasswordFor(m => m.ConfirmPassword)
                            </li>
                            @Html.ValidationMessageFor(m => m.ConfirmPassword)


                        </ol>


                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Post</button>
                    </div>

                </fieldset>
            }

        </div></div>

Any help would be most appreciated! 非常感激任何的帮助! Thanks in advance :) 提前致谢 :)

Turns out it was because i didnt fill out the html begin form statement correctly. 原来是因为我没有正确填写html begin form语句。

 @using (Html.BeginForm("RegisterPartial", "Account", FormMethod.Post , new { @class = "form-horizontal" }))

this works now. 这现在有效。

The problem is your POST action is returning PartialView if modelstate is invalid. 问题是如果modelstate无效,您的POST操作将返回PartialView。 So the partial view will show as entire page instead of model. 因此,部分视图将显示为整个页面而不是模型。 So you can go with ajax forms. 因此,您可以使用Ajax表单。 There is Ajax Helper available for MVC. 有可用于MVC的Ajax帮助器。

Include below script in your page. 在页面中包含以下脚本。

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

Your view will be 您的观点将是

@using (Ajax.BeginForm("RegisterPartial", "Account", new AjaxOptions() {
   OnSuccess = "successCallback"})
{
}

<script>
    function successCallback(response)
    {
         var username='@Model.UserName';
         if(response)
         {
             windows.href.location='/Settings/Data/'+username;
         }
    }
</script>

Your action will be 您的动作将是

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public JsonResultRegisterPartial(RegisterModel model)
{
    if (ModelState.IsValid)
    {      
        //do stuff
         return Json(new {Success=true });
    }
    return Json(new {Success=false});
}

It might be helpful. 这可能会有所帮助。

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

相关问题 子操作不允许执行重定向操作 - Child actions are not allowed to perform redirect actions 错误:子操作不允许执行重定向操作 - Error: Child actions are not allowed to perform redirect actions 子操作不允许执行重定向操作错误 - Child actions are not allowed to perform redirect actions error “不允许子操作执行重定向操作” - “Child actions are not allowed to perform redirect actions” ASP.NET MVC 3 + Razor错误:不允许子操作执行重定向操作 - ASP.NET MVC 3 + Razor Error: Child actions are not allowed to perform redirect actions 不允许子操作执行重定向操作 - 使用ASP.NET MVC 2和Razor时出错 - Child actions are not allowed to perform redirect actions - error while working with ASP.NET MVC 2 and Razor 子操作不允许执行重定向操作。 (使用PartialViews) - Child actions are not allowed to perform redirect actions. (Using PartialViews) 在部分视图控制器中无法正常工作。(错误 - 不允许子操作执行重定向操作。) - doesn't work redirecttoaction in partial view controller.(Error - Child actions are not allowed to perform redirect actions.) 如何解决不允许子操作执行重定向操作,其他答案不解决 - How to fix Child actions are not allowed to perform redirect actions, other answers does not fix MVC4:相同的URL库用于不同的操作 - MVC4: same url base for different actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM