John S
John S

Reputation: 8331

Can I get a SelectList in my view model to render using EditorForModel?

I would like have a listbox of states render using the EditorForModel Html helper. My view model:

public class MyViewModel
{
    public MyewModel()
    {
        States = new SelectList(MyModel.RegionsToSelectList,"Value","Text");
    }

    [DataType(DataType.Text)]
    public string City { get; set; }

    [Display(Name = "States")]
    public SelectList States { get; private set; }
}

In my view I have @Html.EditorForModel() The City renders properly but the States do not render into any sort of list (dropdown or listbox)

If I use @Html.DropDownList("mylistname", Model.States) it renders properly. I would really like to have it render in the ForModel process.

Can this be done?

Upvotes: 0

Views: 1098

Answers (1)

Durgaprasad Budhwani
Durgaprasad Budhwani

Reputation: 977

You need to use the Html.DropdownListFor helper if you want to generate a drop down list. The fact that you have used SelectList as type to some of your properties doesn't mean that the default editor template will render a box. So you will have to write a custom editor template.

You may take a look at the following blog post to see how those default templates are implemented.

Upvotes: 1

Related Questions