我正试图将EmployeeSelect.cfhtml视图中的Employee ID(1(发送到Workers_Comperation controller方法"创建";。我使用了这个问题的答案:使用Html.ActionLink在不同的控制器上调用操作
然而,这些答案都不能很好地发挥作用。当按下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>
工人补偿
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},那么一切都会完美运行。但如果我尝试使用"选择"按钮,它不会。
如果更改控制器,则必须选择具有5个参数的htmlaction重载。控制器没有4个输入参数htmlaction
更换
@Html.ActionLink("Select", "Create", new { id = item.ID })
带有
@Html.ActionLink("Select", "Create", "Workers_Compensation", new {id= item.ID }, null)