locked
SelectList in ViewModel, Dropdown in View, DropDownListFor RRS feed

  • Question

  • User1064071399 posted

    I'm creating a ViewModel (dont have a data layer yet, so I'm "hard coding" the list of "company sizes")
    I want to use DropDownListFor in my View. Obviously I'm doing it wrong, as in my dropdown list I will have 2 items named "System.Web.MVC.SelectListItem

    ViewModel

    public SelectList CompanySizes { get; private set; }
    [DisplayName("Company Size")]
    [Required(ErrorMessage = "Company Size is Required")]
    public string CompanySize { get; set; }
    public CompanySignUpViewModel()
    {
        var companySizes = new List<SelectListItem>
                                                     {
                                                         new SelectListItem
                                                             {
                                                                 Text = "Small Company",
                                                                 Value = "small"
                                                             },
                                                         new SelectListItem
                                                             {
                                                                 Text = "Large Company",
                                                                 Value = "large"
                                                             }
                                                     };
        this.CompanySizes = new SelectList(companySizes);
    }

    In my view:

    <%= Html.DropDownListFor(model=>model.CompanySize, Model.CompanySizes) %>

    Anyone?



    Monday, February 1, 2010 12:45 AM

Answers

All replies