如何在MVC4中的@ Url.Action中传递查询字符串值

时间:2012-10-17 06:47:20

标签: c# asp.net asp.net-mvc-4


我在_Layout.cshtml页面中传递了一个查询字符串值

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


如何传递View Index1.cshtml页面中的值

 <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>

我没有在控制器中获取查询字符串值

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

1 个答案:

答案 0 :(得分:5)

您需要一个名为id的参数作为删除操作方法

Public ActionResult Delete(string id){
     //delete the Request.QueryString line
}