首页 > 解决方案 > 参数字典包含不可为空类型“System.Int32”的参数“parameter_name”的空条目

问题描述

我分别使用两个视图“EmployeeList”和“Edit”。从 EmployeeList 视图中,我调用了一个名为“Edit”的方法,该方法返回名为“Employee”的控制器中的“Edit”视图。“编辑”视图中有一个提交按钮,它调用“员工”控制器中的 Edit_Employee 方法。下面给出各自的观点。

EmployeeList.cshtml
  <tbody>
        @{
            if (Model.Emp_List != null)
            {
                foreach (var item in Model.Emp_List)
                {
                <tr>
                    <td>@item.row_number</td>
                    <td style="display:none">@item.Id</td>
                    <td>@item.Name</td>
                    <td>@item.Designation</td>
                    <td>@item.Department</td>
                    <td>@item.Phone</td>
                    <td>
                        @Html.ActionLink("Edit","Edit", "Employee", new { id = item.Id,flag=1},null)

                    </td>
                </tr>
                    }
                }
            }

    </tbody>
Edit.cshtml
<h2>Edit Employee</h2>
@using (Html.BeginForm("Edit", "Employee", FormMethod.Post))
{
    foreach (var item in Model.Emp_List)
    {
        <div class="form-row">
            <div class="form-group col-md-6">
                <label for="inputname">Name</label>
                <input type="text" class="form-control" value="@item.Name" name="Name" placeholder="name" id="inputname">              
            </div>
            <div class="form-group col-md-6">
                <label for="inputDesignation">Designation</label>
                <input type="text" class="form-control" value="@item.Designation" name="Designation" placeholder="designation" id="inputDesignation">
            </div>
        </div>

        <input type="submit" value="Submit " formaction="Edit_Employee" class="btn btn-primary" />
    }
}

给定的员工控制器方法

  public ActionResult Edit(int id, int flag)
        {
            IList<Employee> emp_editlist = new List<Employee>();
            EmployeeList e = new EmployeeList();
            try
            {
                if (Employeedetailssession.Current.Session["emplist"] != null)
                {
                    emp_editlist = this.GetSession("emplist");
                    emp_editlist = emp_editlist.Where(x => x.Id == id).ToList();
                }
                e.Emp_List = emp_editlist;
                e.Return_Param = 0;
            } 
            catch (Exception)
            { }
            if (flag == 1)
                return View("Edit", e);
            else
                return View("Details", e);
        }

        [HttpPost]
        public ActionResult Edit_Employee(EmployeeList emp)
        {
            EmployeeList empobj = new EmployeeList();
            try
            {                             
                string message = string.Empty;
                HttpClient hc = new HttpClient();

                hc.BaseAddress = new Uri(baseURL);
                var EditRec = hc.PostAsJsonAsync<EmployeeList>("EmployeeApi", emp);
                EditRec.Wait();
                var updatedata = EditRec.Result;
                if (updatedata.IsSuccessStatusCode)
                {
                    empobj = updatedata.Content.ReadAsAsync<EmployeeList>().Result;
                }
                    else
                {
                    empobj.Emp_List = null;
                    empobj.Return_Param = 0;
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.Timeout) { }
                else throw;
            }
            return RedirectToAction("Edit",empobj);
        }

问题是,当我从 EmployeeList 视图调用 Edit 方法时,详细信息已成功加载。但是在进行了一些更改并单击提交按钮后,它不会点击 Edit_Employee 方法,而是显示错误为

'参数字典包含'Vidly.Controllers.EmployeeController'中方法'System.Web.Mvc.ActionResult Edit(Int32,Int32)'的不可空类型'System.Int32'的参数'id'的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。

似乎在单击按钮时它也击中了“编辑”方法。为什么它没有击中正确的方法?我想在提交点击时导航到 Edit_Employee 方法。提前致谢。

标签: c#model-view-controller

解决方案


推荐阅读