不允许子操作执行重定向操作部分视图

时间:2017-07-21 12:21:42

标签: c# asp.net-mvc-5 partial-views

在我的主视图上我有4个部分视图..两个是表格..其他是创建表格。

部分查看表1

@model IEnumerable<ProjectName.Models.code_AutoMake>

<h3>Auto Make List</h3>
<table id="Auto-Make-Table" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th class="col-md-5">
                @Html.DisplayNameFor(model => model.AutoMake)
            </th>
            <th class="col-md-5">
                @Html.DisplayNameFor(model => model.Active)
            </th>
            <th></th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.AutoMake)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Active)
                </td>
                @if (!item.Active)
                {
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.MakeID }) |
                        <a href="#" class="text-info js-automake-activate" data-automake-id="@item.MakeID" data-automake-name="@item.AutoMake">Activate</a>
                    </td>
                }
                else
                {
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.MakeID }) |
                        <a href="#" class="text-danger js-automake-delete" data-automake-id="@item.MakeID" data-automake-name="@item.AutoMake">Deactivate</a>
                    </td>
                }

            </tr>
        }
    </tbody>
</table>

部分查看表2

@model IEnumerable<ProjectName.Models.code_Funding>

<h3>Funding List</h3>
<table class="table table table-bordered table-striped">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Funding)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Active)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Funding)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", "code_Funding",new { id=item.FundID }, null) |
        </td>
    </tr>
}

</table>

部分视图1创建

@model ProjectName.Models.code_AutoMake

@using (Html.BeginForm("Create", "code_AutoMake", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <h3>Add Auto Make</h3>
    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row">
            <div class="col-sm-12 col-md-3">
                @Html.Label("Auto Make")
                @Html.EditorFor(model => model.AutoMake, new {htmlAttributes = new {@class = "form-control"}})
            </div>
            <div class="col-sm-12 col-md-3">
                @Html.Label("Active")
                    <div class="checkbox">
                        @Html.EditorFor(model => model.Active)
                    </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-12 col-md-3">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

部分视图2创建

@model ProjectName.Models.code_Funding

@using (Html.BeginForm("Create", "code_Funding", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <h3>Add Funding</h3>
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row">
            <div class="col-sm-12 col-md-3">
                @Html.Label("Funding")
                @Html.EditorFor(model => model.Funding, new {htmlAttributes = new {@class = "form-control"}})
            </div>
            <div class="col-sm-12 col-md-3">
                @Html.Label("Active")
                <div class="checkbox">
                    @Html.EditorFor(model => model.Active)
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-12 col-md-3">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

主视图

<div id="AutoMake" class="tab-pane fade active in">
    <div id="AutoMake-Index">@{Html.RenderAction("Index", "code_AutoMake");}</div>
    <hr/>
    @{Html.RenderAction("Create", "code_AutoMake");}
</div>
@*Funding*@
<div id="Funding" class="tab-pane fade">
    @{Html.RenderAction("Index", "code_Funding");}
    <hr/>
    @{Html.RenderAction("Create", "code_Funding");}
</div>

现在这里是场景..当我想要创建一个新的autoMake ..我填写表单并点击提交..这很好..直到我回到主视图..特别是这一行:

@{Html.RenderAction("Create", "code_Funding");}

我收到运行时错误说:

  

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

我已经调试了......出于某种原因...... HttpPost Create的{​​{1}}操作被点击了..即使我没有填写code_funding的创建表单..如何那可能吗?

以下是我为code_autoMake创建的方法和code_funding:

code_Funding

code_Funding

code_autoMake

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "FundID,Funding,Active")] code_Funding code_Funding)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.code_Funding.Add(code_Funding);
                db.SaveChanges();
                return RedirectToAction("EditDDL", "tblNewAutos");
            }
        }
        catch (DbEntityValidationException ex)
        {
            foreach (var entityValidationErrors in ex.EntityValidationErrors)
            {
                foreach (var validationError in entityValidationErrors.ValidationErrors)
                {
                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                }
            }
        }
        return RedirectToAction("EditDDL", "tblNewAutos");
    }

为什么当我尝试创建一个新的automake时...两个HttpPost Create方法都被命中了?

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

嗯,问题是如下。在主视图中,您已获得此代码:

...
@{Html.RenderAction("Create", "code_AutoMake");}
...

如果Create {<1}},则会触发ModelState.IsValid == false操作,该操作将使用以下代码行完成:

return RedirectToAction("EditDDL", "tblNewAutos");

这显然是一个坏主意。为什么?您已经在渲染父视图的过程中。儿童行为起初可能有点令人困惑,因为它们不是真正的行动 - 没有客户/服务器通信。你还在服务器端。因此,子操作中不允许重定向。

解决方案

首先,我不太清楚你想要达到什么目标,所以我的解决方案建议可能有点过时,但让我们看看。

选项1

您可能想要使用两种不同的操作。一个是在提交表单时调用的,另一个是从主视图中调用的。后者不应该进行重定向 - 相反,它应该明智地根据ModelState.IsValid选择要呈现的视图,如果这真的是你需要的那样。

选项2

有一种黑客方式允许您从子操作进行重定向。而不是进行重定向,仅存储有关所需重定向的信息,例如HttpContext.Items集合中的信息。然后,实现ActionFilter并在其OnResultExecuted事件中检查重定向请求是否设置为HttpContext.Items。如果是这样,请进行重定向。 ActionFilter应该应用于父操作,而不是应用于子操作。

答案 1 :(得分:0)

Dim maxColNum As String, buffer As String
maxColNum = ActiveSheet.UsedRange.Columns.Count  'get the max column number
buffer = Cells(1, maxColNum).Address(True, False) 'get the address 
buffer = Left(buffer, InStr(buffer, "$") - 1) 'remove row number from it
MsgBox buffer  'show it in messagebox

在这个RenderAction方法中调用GET请求但是,你的控制器只写了post方法。然后你写了

$(".editable").keydown(...);
$(".editable").keyup(function() {
    $(this).blur();
});
$(".editable").blur(...);

并使用[ChildActionOnly]此属性允许通过View中的代码进行受限访问。

检查并回复我..

相关问题