【发布时间】:2021-04-14 15:57:30
【问题描述】:
我正在尝试将员工 ID (1) 从 EmployeeSelect.cfhtml 视图发送到 Workers_Compensation 控制器方法“Create”。我使用了这个问题的答案:Using Html.ActionLink to call action on different controller
但是,这些答案都不能正常工作。当按下 EmployeeSelect 屏幕上的 Select 按钮时,我需要从 https://localhost:44353/Employees/EmployeeSelect 导航到 https://localhost:44353/Workers_Compensation/Create/{ID} 。但是使用该问题的代码(以及我能找到的其他任何地方)最终发生的事情是我导航到 https://localhost:44353/Employees/Create?Length=20 。
我做错了什么?
EmployeeSelect.cshtml
@model IEnumerable<HR_App_V1.Models.Employee>
@{
ViewBag.Title = "Employee Select";
}
<h2>EmployeeSelect</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Last_Name)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.First_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Last_Name)
</td>
<td>
@Html.ActionLink("Select", "Create", new { id = item.ID })
</td>
</tr>
}
</table>
Workers_Compensation
public ActionResult Create(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
System.Diagnostics.Debug.WriteLine("Employee name: " + employee.First_Name);
ViewBag.Claim_Ruling_TypeID = new SelectList(db.Claim_Ruling_Type, "ID", "Claim_Ruling_Type1");
ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "ID");
ViewBag.fName = new SelectList(db.Employees, "First_Name", "First_Name");
ViewBag.lName = new SelectList(db.Employees, "Last_Name", "Last_Name");
ViewBag.WC_TypeID = new SelectList(db.WC_Type, "ID", "WC_Type1");
return View();
}
如果我手动导航到 https://localhost:44353/Workers_Compensation/Create/{ID},那么一切正常。但如果我尝试使用“选择”按钮,它不会。
【问题讨论】:
-
你能显示整个视图吗?
-
我编辑了问题以显示整个视图。
-
很抱歉,我需要你的启动或 webconfig 的端点映射,请。
标签: c# asp.net-mvc model-view-controller navigation