0

I'm trying to display a list of options previously selected.

My model looks like:

 public IEnumerable<SelectListItem> ChildrenOptions { get; set; }
 public List<Guid> ChildrensId { get; set; }

In the controller I populate the IEnumerable with a MultiSelectList from a list and I pass it to the model

MyModel.ChildrenOptions = new MultiSelectList(myList, "Id", "Name");
return View(myModel);

So if I writte in my View

@Html.DisplayFor(modelItem => item.Children, item.ChildrenOptions)

It displays the Guid/Id, but I would like to display a list of the Names. And if I loop the List with a foreach, then I just have the Guids.

Any idea?

Some posts with helpfull related info I've already visited:

Community
  • 1
  • 1
Mario Levrero
  • 3,345
  • 4
  • 26
  • 57
  • It's hard to tell what you're trying to achieve : do you want to display a drop down list ? Or a simple list of names ? – Réda Mattar Nov 08 '13 at 11:08
  • Sorry Réda, Im going to explicit it in the question. In the List/Details View, I need to display the list of names. In the Edit view, when I use a dropdownlistfor, it works Ok. – Mario Levrero Nov 08 '13 at 11:11

1 Answers1

1

If you only need to display a list of names, MultiSelectList is not necessary, as you won't use a drop down list. You just have to get all Names whose Ids are selected :

In your view, before displaying names :

IEnumerable<string> names = item.ChildrenOptions.Where(child => item.ChildrenId.Cast<string>().Contains(child.Value)).Select(child => child.Text);

However, it would be best to create another view model for you list/details view, replacing your MultiSelectList by an IEnumerable<string> (or add this property to your existing view model if you prefer), in order to evaluate your list in your controller. You'd have less noise in your view, and you'd focus on displaying your name list :

In your controller :

myOtherModel.ChildrenOptionNames = myList.Where(child => myOtherModel.ChildrenId.Contains(child.Id)).Select(child => child.Name);
return View(myOtherModel);

In your view :

@Html.DisplayFor(model => model.ChildrenOptionNames) // It will iterate over your list and display each name

Or if you need to apply some special formatting :

@for(int index = 0; index < Model.ChildrenOptionNames.Count; index++)
{
    // apply some formatting, like putting <ul> / <li>
    @Html.DisplayFor(model => model.ChildrenOptionNames[index]);
}
Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
  • Thanks @Réda Mattar .I'm using second option and works. What looks really strange to me is that now in the Model I Have: -A List of Selected Ids -A List of Selected Names -A IEnumerable with all options (Id + Name). Is cleaner, but now Model looks having duplicate data. – Mario Levrero Nov 08 '13 at 14:22
  • This is because you use the same model for two slightly different views : one need a drop down list, the other need a list of names. I don't know if your view has more data than that, but you can refactor this by making two view models, one containing a list of selected ids + a list of selectlistitem (edit), and one containing a list of names (list/details). If some other data are common between them, you can also create a base class containing shared properties. – Réda Mattar Nov 08 '13 at 14:28
  • In that case I will triplicate the number of models, since I have a basic model per entity another one for Editor View and another one for Display View. Looks clean and really structured, but really huge! Thx for your time. – Mario Levrero Nov 08 '13 at 14:57
  • 1
    It seems huge, but you shouldn't worry about this : it takes some effort to build all these, but later you save a lot of time in maintaining you app, when your design is clear. – Réda Mattar Nov 08 '13 at 15:00