简体   繁体   English

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

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

I used modal in my edit page and here is my code: 我在编辑页面中使用了模态,这是我的代码:

In the View 在视图中

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
   Edit
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Edit Event Information</h4>
            </div>
            <div class="modal-body">
                @Html.Action("Edit", "Admin",new { id = Model.events_info_id,@class = "btn btn-warning"})
            </div>
        </div>
    </div>
</div>

This the controller to get the information to be display in the modal: 该控制器获取要显示在模态中的信息:

[ChildActionOnly]
    public ActionResult Edit(int id = 0)
    {
        Events_Info_tbl events_info_tbl = db.Events_Info_tbl.Find(id);
        if (events_info_tbl == null)
        {
            return HttpNotFound();
        }
        return PartialView(events_info_tbl);
    }

This the View of the content of the Modal: 此视图的模态内容为:

@model Online_Ballot.Models.Events_Info_tbl

<script src="~/Scripts/Datepicker.js"></script>
<div class="modal-body" style="color:green">
    <h2>Edit Events</h2>
    @using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.events_info_id)
    <div class="form-group">
        <label for="usr">Event Name:</label>
        @Html.EditorFor(model => model.events_name, new { @class="form-control"})
        @Html.ValidationMessageFor(model => model.events_name)
    </div>
    <div class="form-group">
        <label for="usr">Votation Date:</label>
        @Html.EditorFor(model => model.events_votation_date, new { @id="voters_bdate"})
        @Html.ValidationMessageFor(model => model.events_votation_date)
    </div>
    <div class="form-group">
        <label for="usr">Votation Place:</label>
        @Html.EditorFor(model => model.events_place, new { @class="form-control"})
        @Html.ValidationMessageFor(model => model.events_place)
    </div>

        <div class="form-group">
        <label for="comment">Event Description:</label>
        @Html.TextAreaFor(model => model.events_desc)
    </div>
        <div class="form-group">
        <label for="usr">Is active:</label>
        @Html.EditorFor(model => model.is_active, new { @class="form-control"})
        @Html.ValidationMessageFor(model => model.is_active)
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
    </div>
}

This Controller will update the data: 该控制器将更新数据:

// POST: /Admin/Edit/5
[ValidateAntiForgeryToken]
public ActionResult Edit(Events_Info_tbl events_info_tbl)
{
    if (ModelState.IsValid)
    {
        db.Entry(events_info_tbl).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(events_info_tbl);
}

When I try to run this code I got this error: 当我尝试运行此代码时,出现以下错误:

Child actions are not allowed to perform redirect actions. 子操作不允许执行重定向操作。

But, it updates the data, I guess it not allowed to call RedirectToAction function but I need to. 但是,它会更新数据,我想它不允许调用RedirectToAction函数,但我需RedirectToAction How I am going to fix this one? 我要如何解决这个问题?

Your child action shouldn't be attempting to update anything. 您的子操作不应尝试进行任何更新。 It should be getting data, populating something, and returning some view. 它应该正在获取数据,填充某些内容并返回一些视图。

You probably want to split your action into two actions. 您可能希望将动作分为两个动作。 One for rendering the "Edit" form and another for updating that form. 一个用于呈现“编辑”表单,另一个用于更新该表单。

[HttpPost]
public ActionResult Edit(Events_Info_tbl events_info_tbl)
{
    if (ModelState.IsValid)
    {
        //Save

        return RedirectToAction("Index");
    }

    //Return view
    //NOTE: Make sure your page state is preserved i.e. your modal is open
    return View(events_info_tbl);
}

[ChildActionOnly]
public PartialViewResult RenderEditForm(int id)
{
    //Build form data
    return PartialView("_EditForm");
}

Another note, you have some collision here to how the "Editor" and "Partial" razor helpers can work. 另一个需要注意的地方是,“编辑器”和“部分”剃刀助手的工作方式有些冲突。 Child actions are good for views where it doesn't make sense to fit the data into the controllers model. 子操作对于视图而言是有益的,在视图中将数据放入控制器模型中没有意义。 If the model fits then just use "Editor" or "Partial" 如果模型适合,则只需使用“编辑器”或“部分”

UPDATE UPDATE

This... 这个...

@Html.Action("Edit", "Admin",new { id = Model.events_info_id,@class = "btn btn-warning"})

appears to be pointing to this... 似乎是在指向这个...

public ActionResult Edit(Events_Info_tbl events_info_tbl)

when you actually want it to point to this... 当您实际希望它指向此...

public ActionResult Edit(int id = 0)

edit the controller accordingly in Html.Action Html.Action相应地编辑控制器

@Html.Action("Edit", "MyController",new { id = Model.events_info_id,@class = "btn btn-warning"})

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

相关问题 错误:子操作不允许执行重定向操作 - 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” 子操作不允许执行重定向操作。 (使用PartialViews) - Child actions are not allowed to perform redirect actions. (Using PartialViews) 子操作不允许执行重定向操作MVC4 - Child actions are not allowed to perform redirect actions MVC4 ASP.NET MVC 3 + Razor错误:不允许子操作执行重定向操作 - ASP.NET MVC 3 + Razor Error: Child actions are not allowed to perform redirect actions 在部分视图控制器中无法正常工作。(错误 - 不允许子操作执行重定向操作。) - 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 不允许子操作执行重定向操作 - 使用ASP.NET MVC 2和Razor时出错 - Child actions are not allowed to perform redirect actions - error while working with ASP.NET MVC 2 and Razor 重定向到区域操作中的操作 - Redirect to action in area actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM