将数据从部分视图发布到另一个操作

时间:2016-01-17 19:12:54

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

刚开始使用MVC并尝试执行以下操作:

在我的一个标签上有一个Details.cshtml,其中包含以下部分:

            <!-- DETAILS TAB CONTENT -->
        <div class="tab-pane profile active" id="details-tab">
            @if (ViewBag.ScreenMode == Constants.ScreenMode.View)
            {
                @Html.Partial("_ViewDetails", Model)
            }
            else
            {
                @*@Html.Partial("_EditDetails", Model)*@
                <div id="DetailsEdit">
                    @{Html.RenderPartial("_EditDetails");}
                </div>
            }
        </div>

我的_ViewDetails.cshtml显示详细信息,并有以下内容进入编辑模式:

    @Html.ActionLink("Edit", "Details", new { id = Model.EmployeeId, screenMode = Constants.ScreenMode.Edit })

单击时,实际上选项卡显示部分视图_EditDetails的内容。我的_EditDetails.cshtml看起来像这样:

@using (Html.BeginForm("Edit", "Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.EmployeeId)

        @* fields with editable controls. Left it out here *@

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

并在我的控制器中使用以下方法:

public ActionResult Details(Guid? id, Constants.ScreenMode? screenMode)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Employee employee = employeeManager.Get(id.GetValueOrDefault());
    if (employee == null || employee.EmployeeId == null || employee.EmployeeId == Guid.Empty)
        return HttpNotFound();

    if (screenMode == null)
        screenMode = Constants.ScreenMode.View;

    ViewBag.ScreenMode = screenMode;
    return View(employee);
}


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Guid? id, HttpPostedFileBase upload)
{
    // code here 

    return RedirectToAction("Details", "Employee", new { id = employeeToUpdate.EmployeeId, screenMode = Constants.ScreenMode.View });
}

但是,当我从编辑局部视图中按下保存按钮时,它永远不会在我的控制器中命中我的代码编辑操作。它只会返回到Details操作,它会再次获取信息。我想在部分视图的开头使用以下内容,我可以将帖子转到我的编辑操作:

@using (Html.BeginForm("Edit", "Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))

我错过了什么或者我的想法完全错了?

提前致谢。

1 个答案:

答案 0 :(得分:0)

Aarghhhh,找到了它。

我的Details.cshtml以:

开头
@using (Html.BeginForm("Details", "Employee", null, FormMethod.Post, new { enctype = "multipart/form-data" }))

删除了这个,现在它正在工作。 几天都忽略了这一点。

感谢。