0

I have an IEnumerable of managers Model.managers from this I want to grab the ID value which would be managers.manager_id and the text value which would be managers.manager_name I also have a selected manager value at Model.SelectedManager which holds the manager_id

What I am trying to do is to stuff all of these values into an Html.DropDownListFor

It would need to be a list of all of the manager_id and manager_name and then automatically select the manager stored at Model.SelectedManager is this possible the way I have set it up?

public string SelectedManager { get; set; }
public virtual IEnumerable<managements> managers { get; set; }
James Wilson
  • 5,074
  • 16
  • 63
  • 122
  • 1
    How does your entity model looks like ? Do you have a view model for your view ? – Shyju Feb 18 '16 at 18:44
  • I think this [link](http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor) would be helpful. – SamGhatak Feb 18 '16 at 19:56
  • @Shyju my entity model has a bunch of stuff in it not needed for this specific item but is needed for other parts of the view. Which is why I wanted to see if I could use it rather than creating a new model from scratch just for a drop down list. I am using a view model but the only important part of the model was that it was an IEnumerable that contained two items i needed to pull out of it. – James Wilson Feb 18 '16 at 20:13
  • @SamGhatak Sorry I had already read that page and it wasn't helpful, thank you, however. My needs were creating it from a "non-standard" model that you build into a select list but rather use an entity model that contained the fields I needed. – James Wilson Feb 18 '16 at 20:14
  • Use a view model for your view. Here is a sample http://stackoverflow.com/questions/33398094/asp-mvc-4-request-value-of-dropdownlistfor/33398952#33398952 – Shyju Feb 18 '16 at 20:15

1 Answers1

2

I believe you are trying to do the following:

In a service layer (or the controller), you grab a List of SelectListItem from that managers IEnumerable:

model.ManagerList = managers.Select(i => new SelectListItem()
               {
                    Text = i.manager_name,
                    Value = i.manager_id.ToString()
               }).ToList());

Then, in your view model, you need that SelectedManager for Id and this manager list (of SelectListItem):

public string SelectedManager { get; set; }
public List<SelectListItem> ManagerList {get; set;}

and in your view, you use the ManagerList object with the razor helper:

@Html.DropDownListFor(m => Model.SelectedManager, Model.ManagerList, 
              new {@class = "form-control input-sm whatever-css-class"})

Hope this helps.

Edit: I have always used the selected id as an int and not a string. So I have not tested it as a string actually, and you may want to use it as int if string does not work.

A. Burak Erbora
  • 1,054
  • 2
  • 12
  • 26
  • 1
    Yeah I have to change it to an int but I was ablt to get it to work with a slight modification. `@Html.DropDownListFor(sl => sl.SelectedManager, new SelectList(Model.managers, "imanagement_co_id", "smanagement_co_name"))` seems to work how I need it to just have to add some classes to it. – James Wilson Feb 18 '16 at 20:10
  • Better modification, since you don't make use of a view item (i.e. SelectList) in your service class ;-) Thumbs up. – A. Burak Erbora Feb 18 '16 at 20:12