将下拉选定项目传递回控制器

时间:2015-01-15 15:07:37

标签: c# asp.net-mvc

我正在尝试将下拉列表中的所选项目传递回控制器以触发存储过程。

填充列表的控制器:

public ActionResult Activate()
{
    var query = db.Certificates
                  .Where(a => a.Active == "Y")
                  .Select(cer => cer.CertificateNumber.Substring(0, 4))
                  .Distinct()
                  .OrderBy(cer => cer);

    ViewBag.BoxNumber = new SelectList(query.ToList());

    return View();
}

查看我想要来自的值。

@{
    ViewBag.Title = "Activate";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.DropDownList("BoxNumber", String.Empty)
    <input type="submit" value="Activate" class="btn btn-default" />
}

我希望在以下方面使用它:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Activate(string BoxNumber)
{
    var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["GrandCelebration"].ConnectionString);

    var command = new SqlCommand("ActivateCertificates", connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@BoxNumber", BoxNumber);

    connection.Open();

    command.ExecuteNonQuery();

    connection.Close();

    return RedirectToAction("Activate");
}

未退回所选项目。

0 个答案:

没有答案