Html.BeginForm 在 Controller 中调用正确的 Action
Html.BeginForm call the right Action in Controller
这个问题相关的话题很多,但我还是没弄清楚自己做错了什么。
我有一个数据库,用于管理不同用户对文件夹的访问。在我的视图中,用户可以 select 应该有权访问特定文件夹的员工。然后我想将 selected Employees 传递给 Controller,数据库将在其中更新。
我的问题是:控制器中的正确操作class没有被调用。(我在里面有一个断点)
这是视图
@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>
}
这里是Controller减到最小
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitSelected(EmployeeSelectionViewModel model)
{
return View();
}
更多详细信息:我不确定是什么导致了这个问题,所以这里有更多详细信息。
该视图是 EmployeeSelectionViewModel
的强类型,它将所有员工的 table 表示为一个列表,这里是代码:
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
代表 table 的一行,其中包含所有员工。
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()
第二个参数中删除 "Controller" 单词,不需要它。
@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post, new { encType = "multipart/form-data"}))
Thiago Ferreira 和 haim770 非常感谢!解决方案是使用您的评论的组合。所以:
@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post))
在控制器处
这个问题相关的话题很多,但我还是没弄清楚自己做错了什么。
我有一个数据库,用于管理不同用户对文件夹的访问。在我的视图中,用户可以 select 应该有权访问特定文件夹的员工。然后我想将 selected Employees 传递给 Controller,数据库将在其中更新。
我的问题是:控制器中的正确操作class没有被调用。(我在里面有一个断点)
这是视图
@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>
}
这里是Controller减到最小
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitSelected(EmployeeSelectionViewModel model)
{
return View();
}
更多详细信息:我不确定是什么导致了这个问题,所以这里有更多详细信息。
该视图是 EmployeeSelectionViewModel
的强类型,它将所有员工的 table 表示为一个列表,这里是代码:
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
代表 table 的一行,其中包含所有员工。
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()
第二个参数中删除 "Controller" 单词,不需要它。
@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post, new { encType = "multipart/form-data"}))
Thiago Ferreira 和 haim770 非常感谢!解决方案是使用您的评论的组合。所以:
@using (Html.BeginForm("SubmitSelected", "FolderAccess", FormMethod.Post))
在控制器处