簡體   English   中英

子操作不允許執行重定向操作MVC4

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

我對MVC還是很陌生。 這使我整日發瘋,我一直在閱讀,似乎我的代碼結構中可能存在一些基本錯誤。 但是我正在努力查看它們的確切含義。 據我所知,我什至不使用兒童動作?

在返回的RedirectToAction行上引發了錯誤。

我有一個以表格形式顯示的注冊表。 這種模式可以在許多頁面上看到-因此我創建了一個返回部分視圖的動作。

[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);
}

目前,這是從我的主頁上調用的-但最終會在許多頁面上調用。

<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>

這是局部視圖本身

@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>

非常感激任何的幫助! 提前致謝 :)

原來是因為我沒有正確填寫html begin form語句。

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

這現在有效。

問題是如果modelstate無效,您的POST操作將返回PartialView。 因此,部分視圖將顯示為整個頁面而不是模型。 因此,您可以使用Ajax表單。 有可用於MVC的Ajax幫助器。

在頁面中包含以下腳本。

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

您的觀點將是

@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>

您的動作將是

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

這可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM