为什么 "Http error bad requst 400.0" 显示在 asp.net MVC 中

Why does "Http error bad requst 400.0" displys in asp.net MVC

我是 Asp.net 的新人 我需要你的帮助。我创建了一个 MVC 脚手架项目。一开始,一切正常。然后我更改了主键的名称(从 id 到 EmployeeId)并确保在控制器和视图中更改它们。我能够创建新记录,但是当我单击编辑、详细信息或删除链接时,它显示错误页面 http 错误错误请求 400.0 尽管 url 是正确的。

为Tbl_employeeController截取代码:

enter code here
    // GET: Tbl_Employee/Details/5
    public ActionResult Details(int? EmployeeId)
    {
    if (EmployeeId == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
        Tbl_Employee tbl_Employee = db.Tbl_Employee.Find(EmployeeId);
         if (tbl_Employee == null)
          {
             return HttpNotFound();
          }
         return View(tbl_Employee);
        }

 // GET: Tbl_Employee/Edit/5
        public ActionResult Edit(int? EmployeeId)
        {
            if (EmployeeId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Tbl_Employee tbl_Employee = db.Tbl_Employee.Find(EmployeeId);
            if (tbl_Employee == null)
            {
                return HttpNotFound();
            }
            ViewBag.DepId = new SelectList(db.Tbl_Department, "DepartmentId", "DepName", tbl_Employee.DepId);
            return View(tbl_Employee);
        }

        // POST: Tbl_Employee/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "EmployeeId,EmpName,Position,Nationality,Last_W_D,Head_of_Dep,Status,EmpEmail,EmpPassword,SignaturePath,DepId")] Tbl_Employee tbl_Employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tbl_Employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DepId = new SelectList(db.Tbl_Department, "DepartmentId", "DepName", tbl_Employee.DepId);
            return View(tbl_Employee);
        }

        // GET: Tbl_Employee/Delete/5
        public ActionResult Delete(int? EmployeeId)
        {
            if (EmployeeId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Tbl_Employee tbl_Employee = db.Tbl_Employee.Find(EmployeeId);
            if (tbl_Employee == null)
            {
                return HttpNotFound();
            }
            return View(tbl_Employee);
        }

        // POST: Tbl_Employee/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int EmployeeId)
        {
            Tbl_Employee tbl_Employee = db.Tbl_Employee.Find(EmployeeId);
            db.Tbl_Employee.Remove(tbl_Employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

为 Index.cshtml 截取代码:

<td>
        @Html.ActionLink("Edit", "Edit", new { id = item.EmployeeId }) |
        @Html.ActionLink("Details", "Details", new { id = item.EmployeeId }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.EmployeeId })
    </td>

错误页面: http error bad requst 400.0

恐怕你调试代码时会发现输入参数EmployeeId为空。

我的建议是将您的代码 public ActionResult Details(int? EmployeeId) 更改为 public ActionResult Details(int? id) 并试一试。

原因是在你的MVC项目中,默认路由规则是{controller=Home}/{action=Index}/{id?},这意味着当你发送像GET: Tbl_Employee/Details/5这样的请求时,你需要将你的方法的输入参数设置为[=15] =] 但不是其他参数名称。