Search code examples
asp.net-mvcrazorhtml.listboxfor

How to remove selected property from ListBoxFor


I am using Html.ListBoxFor to populate a list of entries from MVC Model

@Html.ListBoxFor(m => m.DetailsBook, new SelectList(Model.DetailsBook), new { @id = "BookList" })

Now everything works expect that the list items by default selected. Is there anyway I can remove that and just populate these values in list box?


Solution

  • This happens because you're using DetailsBook as both the list and the main expression.

    So what you need to do is create another property, and use that for 'selected values'. e.g.

    Model:

    public string Details { get; set; }
    public List<string> DetailsBook { get; set; }
    

    View:

    @Html.ListBoxFor(m => m.Details, new SelectList(Model.DetailsBook), new { @id = "BookList" })