简体   繁体   中英

Child actions are not allowed to perform redirect actions MVC4

Im very new to MVC and messing around. 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.

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.

 @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. So the partial view will show as entire page instead of model. So you can go with ajax forms. There is Ajax Helper available for MVC.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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