0

I am trying to pass the selected item in a dropdown list back to the controller to fire a stored procedure.

Controller that populates the list:

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();
}

View I want tet the value from.

@{
    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" />
}

Action I want to use it in:

[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");
}

The selected item is not being returned.

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
Doug Farrell
  • 135
  • 1
  • 3
  • 20

0 Answers0