0

I am using Bootstrap-multiselect plugin to add the multiselect dropdown value with checkboxes. Its working fine and I am getting selected value also. Following is my razor code.

@Html.ListBoxFor(model => model.Ex, new SelectList(ViewBag.ExList, "Value", "Text"), new { @class = "form-control", id = "excursion" })

Here is my Model

 public class MyModel
{
   public List<string> Ex { get; set; }
}

 // In here I am getting my selected values from database table.
 // This value is retrieve as comma separated string - item1,item2,item3
 var myselected_List = obj.GetSelectedList();

 // In here I am getting my all dropdown values from database table.
 var myall_List = obj.GetAllList();


 viewBag.ExList = myall_list.Select(x => new SelectListItem()
 {
   Text = x,
   Value = x,

  });

// I want to combine my selected list with alllist and checked the values based on my selected list

I am saving the user selected drop down values in database using comma separated data. such as text1, text2, text3.

In here my ViewBag.ExList contains list with values such as text1, text2,text3,text4, text5.

When same user load the page second time, I want to checked the text1,text2,text3 values as his earlier preferred value in dropdown.

The problem here is, I can get the data from database. I have the full data list too. But I don't know how to achieve this checked checkboxes for user selected items. Can anybody please help me.

Thanks

Kate Fernando
  • 381
  • 1
  • 4
  • 18
  • You need to show your model, in particular, what type is `Ex`, and how you set it –  Aug 28 '17 at 04:29
  • First, creating a 2nd identical `IEnumerable` in the view using `new `SelectList(...)` is pointless extra overhead, and it should be just `@Html.ListBoxFor(m => m.Ex, (IEnumerable)ViewBag.ExList, new { ... })` –  Aug 28 '17 at 05:02
  • 1
    You need to set the value of `Ex` in the controller method before you pass the model to the view. If its `MyModel model = new MyModel{ Ex = new List{ "text1", "text" } }; return View(model);` then those 2 options will be selected when you first render the view. –  Aug 28 '17 at 05:04
  • But _using comma separated data_ is awful practice. Recommend you read [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) –  Aug 28 '17 at 05:05
  • 1
    And since `MyModel` is a view model, then I recommend adding a `public IEnumerable ExList { get; set; }` to it, rather than using `ViewBag` (you should never use `ViewBag` is your already using view model. Then in the view its just `@Html.ListBoxFor(m => m.Ex, Model.ExList, new { ... })` –  Aug 28 '17 at 05:11
  • @StephenMuecke, just for the clarification, I dont clear that procedure. We are getting our selected values based on our viewbag property. Am I right or wrong? What I earlier think is we have to add the selected property true in our creating viewbag.ExList. How our model value getting bind to this? – Kate Fernando Aug 28 '17 at 05:12
  • 1
    No, its the value of the property you bind to (i.e. `Ex`) that determines what is selected. Setting the `Selected` property of `SelectListItem` is ignored when your bind to a property. –  Aug 28 '17 at 05:16
  • If `GetSelectedList()` returns the previous selections, then it would need to be `MyModel model = new MyModel{ Ex = obj.GetSelectedList() }; return View(model);` in your GET method –  Aug 28 '17 at 05:18
  • Thanks @StephenMuecke, Its working now. I have one question. Now the textbox is showing items as item1,item2 and etc by getting the user selected value. And the dropdown values are also checked in here. How it happens? What is the logic that used here to checked the selected checkboxes? – Kate Fernando Aug 28 '17 at 05:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152984/discussion-between-stephen-muecke-and-kate-fernando). –  Aug 28 '17 at 05:27

0 Answers0