Html.BeginForm在Controller中调用正确的Action

时间:2016-03-14 17:31:46

标签: asp.net-mvc html-helper html.beginform

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

我有一个数据库,用于管理不同用户对文件夹的访问权限。在我的视图上,用户可以选择应该有权访问某个文件夹的员工。然后我想将选定的Employees传递给Controller,数据库将在这里更新。

我的问题是:未调用Controller类中的正确操作。(我内部有断点)

以下是视图

@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,它将表格与所有Employees表示为列表,这里是代码:

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表示包含所有Employees的表格的一行。

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

它有一个View,可以为每个Employee

创建复选框
@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,但如上所述,不会调用Action。

编辑:按下按钮

后,我收到HTTP 404 not found错误

2 个答案:

答案 0 :(得分:1)

尝试从Html.BeginForm()第二个参数中删除“控制器”字样,不需要它。

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

答案 1 :(得分:1)

Thiago Ferreira和haim770非常感谢!解决方案是使用您的评论组合。所以:

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

在控制器

相关问题