我将列表绑定到表格,如下图所示。按下删除按钮时,我无法在控制器中获取#ID。请指导某人如何在按下删除按钮时在控制器中获取所选的#ID?
这是我的控制器代码。
[HttpPost]
public ActionResult AddOrUpdateProducts(ProjectViewModel model = null, string Command = null)
{
if (Command == "Delete")
{
//Delete Item from list by #ID from
}
if (Command == "Submit")
{
//Save Items list
}
return View(model);
}
答案 0 :(得分:0)
您可以在视图中使用隐藏字段
<input type="hidden" id="forid" name=""forid"/>
在视图中,据我所知,每个元素都在td内。你可以使用jquery点击功能来改变隐藏字段的值,如下所示
$('.delete').click(function(){
var id=$(this).parents('tr').children(':first').text();// I am supposing that id is placed in first td of row then set this value to hidden field
$('#forid') .val(id);
})
然后在控制器中,您可以使用Request["forid"]
[HttpPost]
public ActionResult AddOrUpdateProducts(ProjectViewModel model = null, string Command = null)
{
if (Command == "Delete")
{
int id=int.Parse(Request["forid"].ToString());
//Delete Item from list by #ID from
}
if (Command == "Submit")
{
//Save Items list
}
return View(model);
}