【问题标题】:Child actions are not allowed to perform redirect actions MVC4子动作不允许执行重定向动作 MVC4
【发布时间】:2015-05-06 10:07:27
【问题描述】:

我对 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.Action() 调用称为子操作的操作(通常您使用[ChildOnlyAction] 属性装饰方法,因此用户无法直接从浏览器调用它)。但是,如果您收到该错误,您似乎正在调用 POST 方法。尝试注释掉 POST 方法以查看 GET 方法是否被命中。
  • 您好斯蒂芬,感谢您的回复。我不确定我是否完全理解。我尝试注释掉 POST 方法,而 GET 方法确实受到了打击。部分在页面加载时正确显示并正确验证等。POST 方法可以满足我的所有需要​​ - 成功完成后重定向除外。

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

原来是因为我没有正确填写 html 开始表单语句。

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

现在可以了。

【讨论】:

    【解决方案2】:

    问题是如果模型状态无效,您的 POST 操作将返回 PartialView。所以局部视图将显示为整个页面而不是模型。因此,您可以使用 ajax 表单。有可用于 MVC 的 Ajax Helper。

    在您的页面中包含以下脚本。

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

    这可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多