user1869870
user1869870

Reputation: 473

How do I use @Html.ListBoxFor method in ASP.Net MVC 3?

I'm seeing "does not contain a definition for..." error.

Here is the code in View

      @model Variant.SystemV.Web.Models._TaggingVM

      @Html.ListBoxFor(x=>x.Tags, Model.SelectedTags, new { @class = "chzn-select", data_placeholder = "Select Tag(s)...", style = "width: 500px" })

Here's my Model

public class _TaggingVM
{
    public IEnumerable<Tag> SelectedTags { get; set; }
    public IEnumerable<Tag> Tags { get; set; }
    public Article Article { get; set; }
}

Upvotes: 0

Views: 11249

Answers (1)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38478

It's because Html.ListBoxFor() extension takes a parameter type of IEnumerable<SelectListItem> and probably the property in your viewmodel doesn't implement it.

You can create the items in your view like this:

@Html.ListBoxFor(x => x.SelectedTags, new SelectList(Model.Tags, "Id", "Text"))

It assumes you have properties named Id and Text in your Tag class. Also you should probably use the SelectedTags property in your expression. It will make sure you collect them back when the form is posted.

Upvotes: 3

Related Questions