badr slaoui
badr slaoui

Reputation: 1063

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

Maybe the question was already asked but never answered!!

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

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

Upvotes: 3

Views: 1500

Answers (1)

user3559349
user3559349

Reputation:

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.

Upvotes: 4

Related Questions