Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From where the deleteconfirmed action get the Id in asp.net mvc

Tags:

asp.net-mvc

Maybe the question was already asked but never answered!!

How the deleteconfirmed method get the id by the post method ?

  • There is no hidden fields containing the Id .

  • I tampered data to change the referer Url so that it not contains the id anymore but the deleteconfirmed action still get the correct id passed during the get .

Then from where does it come ?

here is the code , the get method :

[HttpGet]

public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return RedirectToAction("Index");
        }
        Driver driver= db.Drivers.Find(id);
        if (driver== null)
        {
            return RedirectToAction("Index");
        }
        return View(driver);
    }


    // POST: /Driver/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {

        Driver driver= db.drivers.Find(id);
        db.drivers.Remove(driver);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
like image 835
badr slaoui Avatar asked Nov 20 '25 20:11

badr slaoui


1 Answers

The Html.BeginForm() helper will add route parameters to the forms action action attribute based your route definitions in RouteConfig.cs

Assuming you have the default route url: "{controller}/{action}/{id}", then if you pass a value to (say) 5 to the Delete(int? id) get method, then if you inspect the form tag you will see <form action="/YourController/Delete/5" ...>. Note this will also be added if the model you pass to the view has a property named id.

When you post the form, the DefaultModelBinder read the values of form fields (Request.Form), but also values from route data and query string parameters, so even though you do not have a hidden input for id, it is set from the route parameters.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!