如何将参数传递给程序,通过点击按钮激活?
问题描述:
我有一个博客。在它的主页上有一个主题列表。我想创建一个按钮,它允许用户删除主题(我知道列表中的每个主题的ID,并且该ID是数据库中的关键字段)。如何将参数传递给程序,通过点击按钮激活?
查看/首页/索引:
@{
if (ViewBag.userIsModerator)
using (Html.BeginForm("DeleteTopic", "Home", new { topicId = item.ID }))
{
<input type = "submit" value = "Delete topic" />
}
}
以下过程是在提交后点击启动。
控制器/ HomeController中:
[HttpPost]
public ActionResult DeleteTopic(int topicId)
{
db.DeleteTopic(topicId);
return RedirectToAction("Index", "Home");
}
我的问题是一个主题的ID传递到控制器,因为新{topicId = item.ID}是误差源。有没有人知道,如何达到这个问题,没有new {topicId = item.ID}?
答
在你使用的内部你可以使用隐藏字段。
@using (Html.BeginForm())
{
<input type="hidden" name="topicId" value="5" />
...
}