0

I am developing an ASP.Net MVC 3 Web application and believe my method of returning a list of items to a drop down list in the View is a bit long winded. Let me explain.

I have a ViewModel which contains an Equipment Entity and a SelectList to display a list of Categories.

public class AddEquipmentViewModel
{
    public Equipment equipment { get; set; }
    public SelectList categoryList { get; set; }

}

In the GET create method of my Equipment Controller, I return my ViewModel to the view, see below:

//Add new select list item named 'Please select option' to the top of list
        var categoryList = categoryService.GetAllCategories();
        var categorySelectListItems = categoryList.Select(c => new SelectListItem { Value = c.categoryID.ToString(), Text = c.categoryTitle  }).ToList();
        categorySelectListItems.Insert(0, new SelectListItem { Text = "Please select option", Value = string.Empty });

AddEquipmentViewModel viewModel = new AddEquipmentViewModel
        {
            equipment = new Equipment(),
            categoryList = new SelectList(categorySelectListItems.ToList(), "Value", "Text")
        };

I know I could discard the extra code before I create an instance of my ViewModel and just assign my Category List to the relevant ViewModel property like so

categoryList = new SelectList(categoryService.GetAllCategories(), "categoryID", "categoryTitle")

However, this then just returns a list of categories to my drop down list in my View, whereas, I would like to add a new SelectListItem, ie, "Please select option".

I just feel that my approach to manually adding a new SelectListItem to my SelectList is a bit cumbersome and I would greatly appreciate if someone could share a better method?

Thanks for your time.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • 1. http://stackoverflow.com/questions/2328375/asp-net-mvc-drop-down-list – Mr. L Feb 18 '11 at 16:42
  • 2. http://stackoverflow.com/questions/2396883/asp-net-mvc-populate-a-drop-down-list – Mr. L Feb 18 '11 at 16:42
  • Thanks Liutauras, but none of these examples show how to add a new SelectListItem, which is what I am looking for as I have explained in my question above. Thanks. – tcode Feb 18 '11 at 16:48

1 Answers1

3
<%= Html.DropDownList("name", new SelectList(...), "Your Combobox default message")%>

Hope it helps

alexl
  • 6,841
  • 3
  • 24
  • 29
  • Thanks Alexl, I would prefer to use this approach @Html.DropDownListFor(model => model.equipment.categoryID, Model.categoryList) is there anyway I can add a new SelectListItem to my list within the controller? – tcode Feb 18 '11 at 16:51
  • @Html.DropDownListFor(model => model.equipment.categoryID, Model.categoryList, "default message") should work. Why you want to add it in your controller ? – alexl Feb 18 '11 at 16:53
  • I guess I just thought it would be best practice to do this within the model? – tcode Feb 18 '11 at 16:55
  • 1
    Alexl, I tried your suggestion of @Html.DropDownListFor(model => model.equipment.categoryID, Model.categoryList, "default message") and it works very nicely. I'll go with that. Sorry for the slightly silly question :) – tcode Feb 18 '11 at 17:01