【问题标题】:How could i pass parametres to procedure, activated throught button click?我如何将参数传递给程序,通过按钮单击激活?
【发布时间】:2011-10-05 03:57:00
【问题描述】:

我有一个博客。在它的主页上有一个主题列表。我想创建一个按钮,允许用户删除主题(我知道列表中每个主题的 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 传递给控制器​​,因为 new { topicId = item.ID } 是错误的来源。有谁知道,如何在没有 new { topicId = item.ID } 的情况下解决这个问题?

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    在您的使用中,您可以使用隐藏字段。

    @using (Html.BeginForm())
    {
       <input type="hidden" name="topicId" value="5" />
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多