You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to catch the id in url using mvc

    hi geneious,
    how to catch id in url using mvc. like can i click the edit button in webgrid redirect the page with id.i want catch that id.
    in url show the id is 1. i want catch that in model and controllers

    Thank you
  • #763954
    You have to create action in your controller as follows.

    So that you can get the id directly. Use the type of the id as per your requirement

    // GET: Delete/Delete/5
    public ActionResult Delete(int id)
    if (id == null)
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    PageRepository aPageRepositoryL = new PageRepository();
    Org_Page org_Page = aPageRepositoryL.Get(id);
    if (org_Page == null)
    return HttpNotFound();
    return View(org_Page);

    By Nathan
    Direction is important than speed

  • #763991
    Hai Sadiq,
    You can get the id by creating the method Delete with int id as the parameter.
    If you see, the route-config or the routing file, there it is mentioned that id is default means if you pass the id value to the URL, it will accept your route and if you don't provide then also if will accept the URL as the valid one.
    So when you write the Action method for the Delete, just pass the id value.
    Also you need to make sure that in your View, the id value if integrated and passed.

    Html.ActionLink("Delete", "Delete", idvalue)

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • Sign In to post your comments