-1

I have a dropdownlist inside a view. What i want is, everytime user select the value inside Dropdownlist, the controller take the selected value and use it as parameter for query to database.

Here is the view :

 @using (Html.BeginForm("Index", "Home"))
{

     @Html.DropDownList("Dropdown", new List<SelectListItem>
                       {new SelectListItem {Text="All Assignments", Value = "All" },new SelectListItem {Text="Open Assignments", Value = "Admin"},
                       new SelectListItem{Text="Close Assignments", Value = "Admin"}},"Select One", new { @class = "form-control", style = "width:300px; height:35px;" })

    <h3 style="margin-top:10px;">List of Assignment</h3>
      <table id="myTable" class="table table-hover table-striped">
          //Table Content
      </table>


}

And Here is the Controller:

public ActionResult Index(string username)
        {
            string val = Request.Form["Dropdown"];
            val = Request["Dropdown"];
            string nama = string.Empty;
            if (val == null)
            {
                nama = "admin";
            }
            else
            {
                nama = val;
            }

            if (Session["username"] != null)
            {

                string user = Session["username"].ToString();
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                string sqlQuery = @"select Animals, Gender from dbo.Animals where Pemilik = @user";

                //string h = x => x.
                SqlCommand cmd = new SqlCommand(sqlQuery, conn);
                cmd.Parameters.AddWithValue("@user", nama);
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                conn.Close();
                return View(dt);
            }
            else
            {
                return RedirectToAction("Login", "Login");
            }

        }

I tried using FormCollection to get the dropdownlist selected value, but is says Object Reference Not Set To An Instance Of object. So i decide to use Request.Form. However, im still not sure that string val contains value.

Icon
  • 103
  • 9
  • How are you sending the selected value to controller ? Is it through form submit ? or ajax ? – Shyju Sep 04 '17 at 14:13
  • @Shyju: I refer to this question to send the valut into controller. https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc – Icon Sep 04 '17 at 14:17
  • If you are doing a form submit, your code `string val = Request.Form["Dropdown"];` should work. Why do you think it is not working ? Did you debug your code ? Put breakpoints and debug your code. – Shyju Sep 04 '17 at 14:17
  • @Shyju: I did debug my code, when i debug my code, and check string val value. Its Null. – Icon Sep 04 '17 at 14:23
  • @Shyju: The above view, is directly opened when user log in to the web, so even the user did't select the value, i have been set the default parameter on the controller for the SQL Query – Icon Sep 04 '17 at 14:27

1 Answers1

0

If you submit your form to the action method, your code string val = Request.Form["Dropdown"]; will work. But since you want to send the value everytime user changes the option, you need to submit the form via javascript. You can listen to the change event on the SELECT element and submit the form.

The below javascript code will do tit.

$(function () {

    $("#Dropdown").change(function(e) {
        $(this).closest("form").submit();
    });

});

Now put a breakpoint in your action method and inspect the value of val variable after it executes the line string val = Request.Form["Dropdown"];.

Alternatively, you can add a parameter with name matching to the name of your SELECT element, to your action method.

public ActionResult Index(string username,string Dropdown)
{
  string val = Dropdown;
  // to do : Return something
}
Shyju
  • 214,206
  • 104
  • 411
  • 497