Answered by:
DropdownList For doesn't select expected item

Question
-
User-1323023755 posted
Hi, I have a dropdown list in view that will not select the item from the database. I have it in a for each loop on the view.
The structure of this data is job --> job form. Each job may have 1 or many job forms. The example has 1 job form. This setup is quite complicated so I'll do my best to explain it.
I have a view model for the job and the job view model has a list of job form view model(s). As a test I setup a view with a job form view only and the correct item is selected on load in the drop down list. When I have the job --> job form view and I use the for loop for each job form, the top item is selected and should be selecting the item with the value from the database. I don't know where the disconnect is happening. I don't want to use an editor template for multiple reasons (i'm following this example to save hierarchical data https://www.pluralsight.com/guides/asp.net-mvc-getting-default-data-binding-right-for-hierarchical-views). I want to to loop through the model's job forms and use the Html.DropDownListFor and have the proper item selected (meaning the value from the database) in the drop down. The job form has a value of "20" for the id field, and that item in the select list is not selected when looping through the job forms like this. As a test, I've written the value to HTML using "@Model.job_offset_edit_job_form_viewModel[i].job_form.pressSiteDictionaryID" and "20" is written, so the value is correct, it just won't match to the drop down list using the foreach iterator.
I know this might be confusing and will probably subject myself to some flak, i might deserve it, but that is the best way I could do to explain it. Any help is appreciated, thanks, Joe
here is the view code :
@model lithopress_KISv2.ViewModels.job_offset_edit_viewModel @for (var i = 0; i < Model.job_offset_edit_job_form_viewModel.Count(); i++) { @Html.DropDownListFor(m => Model.job_offset_edit_job_form_viewModel[i].job_form.pressSiteDictionaryID, Model.sl_press, new {@class = "form-control"}) }
here is the controller:
public ActionResult EditJobForm(int id =0) { job_offset_edit_viewModel _jobOffSet = new job_offset_edit_viewModel(); List<job_offset_edit_job_form_viewModel> _jfList = new List<job_offset_edit_job_form_viewModel>(); List<SelectListItem> selectListItems = new List<SelectListItem>(); selectListItems.Add(new SelectListItem { Text = "41A", Value = "12" }); selectListItems.Add(new SelectListItem { Text = "81D", Value = "20" }); SelectList selectList = new SelectList(selectListItems, "Value", "Text"); _jobOffSet.sl_press = selectList; job_offset_edit_job_form_viewModel jobFormVM = new job_offset_edit_job_form_viewModel(); job_form jf = new job_form(); jf.id = 124; jf.pressSiteDictionaryID = 20; jobFormVM.job_form = jf; _jfList.Add(jobFormVM); _jobOffSet.job_offset_edit_job_form_viewModel = _jfList; return View(_jobOffSet); }
Here are the view models:
public class job_offset_edit_job_form_viewModel { public List<site_dictionary> press_list { get; set; } //1 public SelectList sl_press { get; set; } public job_form job_form { get; set; } //public List<vendor> vendor { get; set; } //public List<status> status { get; set; } //public List<employee> salesperson { get; set; } }
public class job_offset_edit_viewModel { public List<customer> customer { get; set; } public List<vendor> vendor { get; set; } public List<status> status { get; set; } public List<employee> salesperson { get; set; } public SelectList sl_status { get; set; } public SelectList sl_customer { get; set; } public SelectList sl_salesperson { get; set; } public Job_Offset job_offset { get; set; } public string srch_querystring { get; set; } public string x_jobNumber { get; set; } public string x_jobID { get; set; } public string x_customerID { get; set; } public string x_customerPONumber { get; set; } public string x_jobDescription { get; set; } public string x_status { get; set; } public List<job_offset_edit_job_form_viewModel> job_offset_edit_job_form_viewModel { get; set; } public List<site_dictionary> stock_type_list { get; set; } //2 public List<site_dictionary> stock_size_list { get; set; } //3 public List<site_dictionary> stock_ordered_from_list { get; set; } //5 public List<site_dictionary> layout_list { get; set; } //hardcoded public List<site_dictionary> press_list { get; set; } //1 public List<site_dictionary> coating_list { get; set; } //4 public SelectList sl_stock_type { get; set; } public SelectList sl_stock_size { get; set; } public SelectList sl_stock_ordered_from { get; set; } public SelectList sl_layout { get; set; } public SelectList sl_press { get; set; } public SelectList sl_coating { get; set; } }
Wednesday, September 4, 2019 3:10 AM
Answers
-
User-474980206 posted
just put a breakpoint in the loop and check the values. view source to see what is rendered, maybe some client code is changing it.
you could also cleanup your code. the following
List<SelectListItem> selectListItems = new List<SelectListItem>(); selectListItems.Add(new SelectListItem { Text = "41A", Value = "12" }); selectListItems.Add(new SelectListItem { Text = "81D", Value = "20" }); SelectList selectList = new SelectList(selectListItems, "Value", "Text");
could be simplified to:
public class job_offset_edit_job_form_viewModel { public List<site_dictionary> press_list { get; set; } //1 public IEnumerable<SelectListItem> sl_press { get; set; } public job_form job_form { get; set; } } .... var selectList = new List<SelectListItem> { new SelectListItem { Text = "41A", Value = "12" }, new SelectListItem { Text = "81D", Value = "20" } };
and if in a database, a linq query could be used.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 4, 2019 10:46 PM -
User-17257777 posted
Hi jmetape,
If I understand right, since your job_form has a record with a pressSiteDictionaryID of 20, then you want to select the option with a value of "20" in the selectlist. I make some changes to your codes.
Model:
public class job_offset_edit_viewModel { //... public List<job_offset_edit_job_form_viewModel> job_offset_edit_job_form_viewModel { get; set; } //... public List<SelectListItem> sl_press { get; set; } //... }
Controller:
public ActionResult EditJobForm(int id = 0) { job_offset_edit_viewModel _jobOffSet = new job_offset_edit_viewModel(); List<job_offset_edit_job_form_viewModel> _jfList = new List<job_offset_edit_job_form_viewModel>(); List<SelectListItem> selectListItems = new List<SelectListItem>(); selectListItems.Add(new SelectListItem { Text = "41A", Value = "12" }); selectListItems.Add(new SelectListItem { Text = "81D", Value = "20" }); //SelectList selectList = new SelectList(selectListItems, "Value", "Text"); _jobOffSet.sl_press = selectListItems; job_offset_edit_job_form_viewModel jobFormVM = new job_offset_edit_job_form_viewModel(); job_form jf = new job_form(); jf.id = 124; jf.pressSiteDictionaryID = 20; foreach (var item in _jobOffSet.sl_press) { if (int.Parse(item.Value) == jf.pressSiteDictionaryID) { item.Selected = true; } } jobFormVM.job_form = jf; _jfList.Add(jobFormVM); _jobOffSet.job_offset_edit_job_form_viewModel = _jfList; return View(_jobOffSet); }
It finally works, the option { Text = "81D", Value = "20" } is selected. If I understand wrong, please point out and tell us what you want to achieve.
Best Regards,
Jiadong Meng.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 5, 2019 7:20 AM
All replies
-
User-474980206 posted
the code should be:
@for (var i = 0; i < Model.job_offset_edit_job_form_viewModel.Count(); i++) { @Html.DropDownListFor(m => m.job_offset_edit_job_form_viewModel[i].job_form.pressSiteDictionaryID, Model.sl_press, new {@class = "form-control"}) }
Wednesday, September 4, 2019 2:22 PM -
User-1323023755 posted
Thank you for the response. Unfortunately, that didn't work.
I'm going bonkers with this!
Joe
Wednesday, September 4, 2019 9:07 PM -
User-474980206 posted
just put a breakpoint in the loop and check the values. view source to see what is rendered, maybe some client code is changing it.
you could also cleanup your code. the following
List<SelectListItem> selectListItems = new List<SelectListItem>(); selectListItems.Add(new SelectListItem { Text = "41A", Value = "12" }); selectListItems.Add(new SelectListItem { Text = "81D", Value = "20" }); SelectList selectList = new SelectList(selectListItems, "Value", "Text");
could be simplified to:
public class job_offset_edit_job_form_viewModel { public List<site_dictionary> press_list { get; set; } //1 public IEnumerable<SelectListItem> sl_press { get; set; } public job_form job_form { get; set; } } .... var selectList = new List<SelectListItem> { new SelectListItem { Text = "41A", Value = "12" }, new SelectListItem { Text = "81D", Value = "20" } };
and if in a database, a linq query could be used.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 4, 2019 10:46 PM -
User-17257777 posted
Hi jmetape,
If I understand right, since your job_form has a record with a pressSiteDictionaryID of 20, then you want to select the option with a value of "20" in the selectlist. I make some changes to your codes.
Model:
public class job_offset_edit_viewModel { //... public List<job_offset_edit_job_form_viewModel> job_offset_edit_job_form_viewModel { get; set; } //... public List<SelectListItem> sl_press { get; set; } //... }
Controller:
public ActionResult EditJobForm(int id = 0) { job_offset_edit_viewModel _jobOffSet = new job_offset_edit_viewModel(); List<job_offset_edit_job_form_viewModel> _jfList = new List<job_offset_edit_job_form_viewModel>(); List<SelectListItem> selectListItems = new List<SelectListItem>(); selectListItems.Add(new SelectListItem { Text = "41A", Value = "12" }); selectListItems.Add(new SelectListItem { Text = "81D", Value = "20" }); //SelectList selectList = new SelectList(selectListItems, "Value", "Text"); _jobOffSet.sl_press = selectListItems; job_offset_edit_job_form_viewModel jobFormVM = new job_offset_edit_job_form_viewModel(); job_form jf = new job_form(); jf.id = 124; jf.pressSiteDictionaryID = 20; foreach (var item in _jobOffSet.sl_press) { if (int.Parse(item.Value) == jf.pressSiteDictionaryID) { item.Selected = true; } } jobFormVM.job_form = jf; _jfList.Add(jobFormVM); _jobOffSet.job_offset_edit_job_form_viewModel = _jfList; return View(_jobOffSet); }
It finally works, the option { Text = "81D", Value = "20" } is selected. If I understand wrong, please point out and tell us what you want to achieve.
Best Regards,
Jiadong Meng.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 5, 2019 7:20 AM -
User-1323023755 posted
Thanks Bruce, between your responses and Jiadong Meng's -- i got it to work. you were correct--something was changing , I had couple things out of order. Thanks so much.
Thursday, September 5, 2019 3:30 PM -
User-1323023755 posted
Thank you, jiadongm. Works great. thank you for the response
Thursday, September 5, 2019 3:31 PM