How to pass querystring value in @Url.Action in MVC4

10,363

You need an argument called id for your delete action method

Public ActionResult Delete(string id){
     //delete the Request.QueryString line
}
Share:
10,363
kk1076
Author by

kk1076

Sitecore Developer

Updated on August 06, 2022

Comments

  • kk1076
    kk1076 almost 2 years


    I passed a querystring value in _Layout.cshtml page

     <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
     <li>@Html.ActionLink("Shopping", "Index1", new { controller = "Shopping", UserID = "Admin" })</li> 
    


    How to pass the value in the View Index1.cshtml page

     <a href="@Url.Action("Delete","Shopping", new { id = Request.QueryString["UserID"] })">
     <img alt="removeitem" style="vertical-align: middle;" height="17px" src="~/Images/remove.png" title="remove" />
     </a>
    

    I am not getting the querystring value in my controller

    public ActionResult Delete()
    {
        string id = Request.QueryString["UserID"];
        int records = DeleteItem(id);
        if (records > 0)
        {
            return RedirectToAction("Index", "Shopping");
        }
        else
        {
            ModelState.AddModelError("", "Can Not Delete");
            return View("Index");
        }
    }
    
    public int DeleteItem(string id)
    {
        con.Open();          
        SqlCommand cmd = new SqlCommand("delete from Cpecial_Shopping_Cart_tbl where [User ID]='" + id + "'", con);         
        return cmd.ExecuteNonQuery();
    }