Html.BeginForm 在控制器中调用正确的操作


有很多

与这个问题相关的主题,但我仍然没有弄清楚我做错了什么。

我有一个数据库,我可以在其中管理不同用户对文件夹的访问。在我的视图上,用户可以选择应该有权访问特定文件夹的员工。然后,我想将选定的员工传递给控制器,数据库将在其中更新。

我的问题是:控制器类中的正确操作未被调用。(我里面有一个断点)

这是视图

@model DataAccessManager.Models.EmployeeSelectionViewModel
@{
    ViewBag.Title = "GiveAccessTo";
}
@using (Html.BeginForm("SubmitSelected", "FolderAccessController", FormMethod.Post, new { encType = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.fr_folder_uid_fk)
<div class="form-horizontal">
<input type="submit" value="Save" id="submit" class="btn btn-default" />
            <table id="tableP">
                <thead>
                    <tr>
                        <th>Selection</th>
                        <th>Second Name</th>
                        <th>First Name</th>
                        <th>Department</th>
                    </tr>
                </thead>
                <tbody id="people">
                    @Html.EditorFor(model => model.People)       
                </tbody>
            </table>
        </div>
    </div>
</div>
}

这是控制器减少到最低限度

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitSelected(EmployeeSelectionViewModel model)
{
    return View();
}

更多详细信息:我不确定是什么导致了问题,所以这里有更多细节。该视图被强类型化为EmployeeSelectionViewModel,它将所有员工的表重新预设为列表,这是代码:

public class EmployeeSelectionViewModel
{
    public List<SelectEmployeeEditorViewModel> People { get; set; }
    public EmployeeSelectionViewModel()
    {
        this.People = new List<SelectEmployeeEditorViewModel>();
    }
    public Int64 fr_folder_uid_fk { get; set; }
    public IEnumerable<string> getSelectedIds()
    {
        // Return an Enumerable containing the Id's of the selected people:
        return (from p in this.People where p.Selected select p.fr_mavnr_fk).ToList();
    }
}

SelectEmployeeEditorViewModel表示表的一行,其中包含所有员工。

public class SelectEmployeeEditorViewModel
{
    public bool Selected { get; set; }
    public string fr_mavnr_fk { get; set; }
    public string firstName { get; set; }
    public string secondName { get; set; }
    public string dpt { get; set; }
}

它有一个视图,为每个员工创建复选框

@model DataAccessManager.Models.SelectEmployeeEditorViewModel
<tr>
    <td style="text-align:center">
        @Html.CheckBoxFor(model => model.Selected)
        @Html.HiddenFor(model => model.fr_mavnr_fk)
    </td>
    <td>
        @Html.DisplayFor(model => model.secondName)
    </td>
    <td>
        @Html.DisplayFor(model => model.firstName)
    </td>
    <td>
        @Html.DisplayFor(model => model.dpt)
    </td>
</tr>

当我按下"提交"按钮时,会在浏览器中调用/FolderAccessController/SubmitSelected URL,但如前所述,不会调用该操作。

编辑:按下按钮后收到HTTP 404未找到错误

尝试从Html.BeginForm()第二个参数中删除"控制器"一词,它不是必需的。

@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post, new { encType = "multipart/form-data"}))

蒂亚戈·费雷拉和 haim770 非常感谢!解决方案是使用您的评论组合。所以:

@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post))

在控制器处

最新更新