How to achieve edit and delete on Webgrid of MVC3 Razor?

强颜欢笑 提交于 2019-12-03 21:12:14

@Yasser, it is very dangerous to implement a DELETE via a GET link. A search engine crawling the page might delete all your information.

It is much better to implement a POST operation. Here is an example:

In the View:

@functions{
  string Delete(dynamic p)
  {
    string actionController = Url.Action("Delete", "Admin", new {id=p.AccountId});
    return "<form style='display:inline;' method='post' action='" + actionController + "'><input type='submit' value='Delete' onclick=\"return confirm('Are you sure?')\"/></form>";
  }
}

...
grid.Column(header: "", format: p => Html.Raw(Delete(p)))

In the Controller:

[HttpPost]
public ActionResult Delete(int id)
{
   PerformDelete(id);
   return RedirectToAction("Index");
}

Here is something you can start with,

You will have to first generate two action link called "Edit" and "Delete" along with each record in the webgrid.

See this tutorial for that.

something like this

grid.Column(format: (item) => Html.ActionLink("Edit", "ActionName", new { param1 = "send id here", param2 = "xtra param" }))
grid.Column(format: (item) => Html.ActionLink("Delete", "ActionName2", new { param1 = "hello", param2 = "bye" }))

Hope this helps.

You can try this inline editable gridview using asp.net mvc and knockoutjs: www.anhbui.net/blog?id=kojs-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!