简体   繁体   中英

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; }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM