0

Filter records in index view using dropdown list . How replace xxxxxxx with selectedValue from dropdownlist to filter. I am not sure how to select the selectedValue from dropdown list in the view. The controller code and the view code is below. I know its a small problem. But I am trying to find for some hours.The filter is working fine if i replace with xxxxxx with 1 , 2 etc.But I want to change xxxxx with selectedValue from Dropdown list.

controller code

ViewBag.doctorsid = new SelectList(db.doctors, "doctorsid", "doctorsname",selectedValue);
return View(appointments.Where(d => d.doctorsid == xxxxxxx).ToList());

view code

@Html.DropDownList("Doctorsid","Select Doctor")
yesganesh
  • 35
  • 7
  • Show the signature of your method (its needs a property `int Doctorsid` to bind to). But this is a terrible approach. Bind your dropdownlist to a model property (refer [this answer](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) for an example –  Sep 15 '17 at 10:04

2 Answers2

0

Use script given bellow

Script

 $("#Doctorsid").change(function () {
   var doctorsid = $('#Doctorsid').val();
   var url = "/ControllerNmae/ActionResultName/+doctorsid ;
   window.location.href = url;
});
Alsamil Mehboob
  • 384
  • 2
  • 6
  • 24
  • As you suggested I made small changes and included javascript as follows. But the value is not pkckedup. – yesganesh Sep 15 '17 at 15:14
0

Use a Model with variables for list and drop down value instead of passing list directly to view Controller:

ViewBag.ClientTypeID = new SelectList(db.ClientType.Where(ct => ct.IsDeleted != true), "ID", "ClientTypeName");

View:

@Html.DropDownListFor(m => m.ClientTypeID, new SelectList(ViewBag.ClientTypeList, "Value", "Text"), new { @class="drop"})
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Karthik U
  • 1
  • 1