views:

160

answers:

3

i use this code to fill dropdownlist

ViewData["projectType"] = new SelectList  
  (_dataManager.Project.ProjectTypeList(), "Id", "Name");

but what i must do if i want to use not one table column but two or more columns? for example

ViewData["projectType"] = new SelectList  
  (_dataManager.Project.ProjectTypeList(), "Id", "Name1"+"Name2");
A: 

You can always fill SelectList manually.

Something like (i didn't test it):

var list = new SelectList();
var data = _dataManager.Project.ProjectTypeList();

foreach (var d in data){
  list.Add(new SelectListItem  
    (string.Format("{0} {1}",d.Name1,d.Name2), d.Id, isChecked));
}
ViewData["projectType"] = list;
Arnis L.
+4  A: 

Just add a property to your model ViewModel:

public partial class Project
{
    public string FullName 
    {
        get { return Name1 + Name2; }
    }
}

and use this property:

ViewData["projectType"] = new SelectList(
    _dataManager.Project.ProjectTypeList(), 
    "Id", 
    "FullName"
);
Darin Dimitrov
If `FullName` is necessary for presentation only, this should be avoided.
Arnis L.
That's why a ViewModel should be used. Model -> ViewModel -> View. The ViewModel is what you present to the View so such property is allowed there.
Darin Dimitrov
Now it sounds better. :)
Arnis L.
A: 

You should be able to do something similar to this:

ViewData["service_lines"] = new SelectList  
     (repository.SubcontractService_Lines(), "Id", "Name"); 

In Repository:

    public class SelectService_LineItem  
            {
                public string Name { get; set; }
                public Guid Id { get; set; }
            }

    public IEnumerable<SelectService_LineItem> SubcontractService_Lines() 
    {
        return
            from sl in db.service_lines
            select new SelectService_LineItem
            {
                Name = sl.service_line_name + "" + sl.service_line_name2,
                Id = sl.service_line_id
            };
    }
RememberME