Home > Software engineering >  Why does "Http error bad requst 400.0" displys in asp.net MVC
Why does "Http error bad requst 400.0" displys in asp.net MVC

Time:04-11

I'm new in Asp.net I need your help please. I have created an MVC scaffolding project. At the beginning, Everything was working fine. then I changed the name of Primary keys (was id to EmployeeId) and make sure to change them in the controller and views. I am able to create new records but when I click on edit, details, or delete links it shows error page http error bad request 400.0 althugh the url is correct.

snipping Code for 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");
        }

snipping Code for 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>

the error page: http error bad requst 400.0

CodePudding user response:

I'm afraid when you debug you code you will find the input parameter EmployeeId is null.

My suggestion is changing your code public ActionResult Details(int? EmployeeId) to public ActionResult Details(int? id) and have a try.

The reason is that in your MVC project, default route rule is {controller=Home}/{action=Index}/{id?} that means when you send request like GET: Tbl_Employee/Details/5, you need to set the input parameter of your method as int? id but not other parameter name.

  • Related