ListBoxFor not populating with selected items

Posted by user576838 on Stack Overflow See other posts from Stack Overflow or by user576838
Published on 2012-11-24T23:40:26Z Indexed on 2012/11/25 17:04 UTC
Read the original article Hit count: 280

I've see this question asked a couple of other times, and I followed this after I tried things on my own with the MS music store demo to no avail, but I still can't get this to work. I've also noticed when I look at my MultiSelectList object in the viewmodel, it has the correct items in the selected items property, but if I expand the results view, it doesn't have any listboxitem with the selected value. What am I missing here? I feel like I'm taking crazy pills! Thanks in advance.

model:

public class Article

    {
        public int ArticleID { get; set; }
        public DateTime? DatePurchased { get; set; }
        public DateTime? LastWorn { get; set; }
        public string ThumbnailImg { get; set; }
        public string LargeImg { get; set; }


        public virtual List<Outfit> Outfits { get; set; }
        public virtual List<Tag> Tags { get; set; }
    }

viewmodel:

public class ArticleViewModel
{
    public int ArticleID { get; set; }
    public List<Tag> Tags { get; set; }
    public MultiSelectList mslTags { get; set; }
    public virtual Article Article { get; set; }

    public ArticleViewModel(int ArticleID)
    {
        using (ctContext db = new ctContext())
        {
            this.Article = db.Articles.Find(ArticleID);
            this.Tags = db.Tags.ToList();
            this.mslTags = new MultiSelectList(this.Tags, "TagID", "Name", this.Article.Tags);

        }
    }

}

controller:

 public ActionResult Index()
    {
        ArticleIndexViewModel vm = new ArticleIndexViewModel(db);

        return View(vm);
    }

view:

    @model ClosetTracker.ArticleViewModel
        @using (Html.BeginForm())
        {

            <img id="bigImg" src="@Model.Article.ThumbnailImg" alt="img" />
            @Html.HiddenFor(m => m.ArticleID);

            @Html.LabelFor(m => m.Article.Tags)
 @*   @Html.ListBoxFor(m => m.Article.Tags, Model.Tags.Select(t => new SelectListItem { Text = t.Name, Value = t.TagID.ToString() }), new { Multiple = "multiple" })
*@  
   @Html.ListBoxFor(m => m.Article.Tags, Model.mslTags);

            @Html.LabelFor(m => m.Article.LastWorn)    
            @Html.TextBoxFor(m => m.Article.LastWorn, new { @class = "datepicker" })
            @Html.LabelFor(m => m.Article.DatePurchased)            
            @Html.TextBoxFor(m => m.Article.DatePurchased, new { @class = "datepicker" })

            <p>
                <input type="submit" value="Save" />
            </p>
        }

EDITED

Ok, I changed around the constructor of the MultiSelectList to have a list of TagID in the selected value arg instead of a list of Tag objects. This shows the correct tags as selected in the results view when I watch the mslTags object in debug mode. However, it still isn't rendering correctly to the page.

 public class ArticleViewModel
    {
        public int ArticleID { get; set; }
        public List<Tag> Tags { get; set; }
        public MultiSelectList mslTags { get; set; }
        public virtual Article Article { get; set; }

        public ArticleViewModel(int ArticleID)
        {
            using (ctContext db = new ctContext())
            {
                this.Article = db.Articles.Find(ArticleID);
                this.Tags = db.Tags.ToList();
                this.mslTags = new MultiSelectList(this.Tags, "TagID", "Name",  this.Article.Tags.Select(t => t.TagID).ToList());

            }
        }

    }

© Stack Overflow or respective owner

Related posts about asp.net-mvc-3

Related posts about entity-framework-4