【问题标题】:ASP.Net MVC Passing data from a view to a controller in a different folderASP.Net MVC 将数据从视图传递到不同文件夹中的控制器
【发布时间】: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


【解决方案1】:

如果你改变一个控制器,你必须选择带有 5 个参数的 htmlaction 重载。没有带有控制器的4个输入参数htmlaction

替换

 @Html.ActionLink("Select", "Create", new { id = item.ID }) 

 @Html.ActionLink("Select", "Create", "Workers_Compensation", new {id= item.ID }, null) 

【讨论】:

  • 哦,哇,我看到大多数人都使用空值,但我认为这是可选的。比你!
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多