yeomandev
yeomandev

Reputation: 11796

Why won't Html.ListBoxFor() highlight current selected items?

I am trying to understand why my Html.ListBoxFor() is not highlighting current selected items when the view loads.

I have a database model:

    public class Issue
    {
        [Key]
        public int      IssueId     { get; set; }
        public int      Number      { get; set; }
        public string   Title       { get; set; }
        public DateTime Date        { get; set; }

        public virtual ICollection<Creator> Creators { get; set; }
    }

    public class Creator
    {
        [Key]
        public int      CreatorId   { get; set; }
        public string   FirstName   { get; set; }
        public string   MiddleName  { get; set; }
        public string   LastName    { get; set; }

        public virtual ICollection<Issue> Issues { get; set; }
    }

    public class Icbd : DbContext
    {
        public DbSet<Issue> Issues { get; set; }
        public DbSet<Creator> Creators { get; set; }
    }

I then have an editing model:

public class IssueEditModel
{
    public Issue Issue { get; set; }
    public IEnumerable<Creator> Creators { get; set; }
    public IEnumerable<Creator> SelectedCreators { get {return Issue.Creators;} }
}

Then, in my controller I populate IssueEditModel:

    public ActionResult EditIssue( int id = 0 )
    {
        IssueEditModel issueEdit = new IssueEditModel{
            Creators = db.Creators.ToList(),
            Issue = new Issue{ Creators = new List<Creator>()},
        };
        if (id > 0)
        {
            issueEdit.Issue = db.Issues.Include("Creators").Where(x => x.IssueId == id).Single();
        }

        return View(issueEdit);
    }

This populates all objects correctly (as far as I can tell, anyway.) In my View, I am writing a listbox like this:

    <%: Html.ListBoxFor(
                x => x.SelectedCreators, 
                new SelectList(
                    Model.Creators, 
                    "CreatorId", 
                    "LastName"
        )
            )%>

This lists all the options correctly, but I cannot get the currently select items to highlight. I almost want to write my own Html Helper because this is such a simple operation, I don't understand why this is being so difficult.

Why wont the Html Helper highlight the current items?

Upvotes: 0

Views: 4246

Answers (2)

Bob Quinn
Bob Quinn

Reputation: 139

The example submitted by Darin is ALMOST correct, but there is a slight error. Presently, his example will preselect ALL creators! However, the desired result is to only preselect the creators associated with a particular instance of Issue.

So this:

IssueEditModel issueEdit = new IssueEditModel 
{
    Creators = db.Creators,
    SelectedCreatorIds = db.Creators.Select(x => x.CreatorId)
};

Should be this:

IssueEditModel issueEdit = new IssueEditModel 
{
    Creators = db.Creators,
    SelectedCreatorIds = CurrentIssue.Creators.Select(x => x.CreatorId)
};

Where CurrentIssue is an instantiation of the Issue class (presumably previously populated from the datastore).

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

You need a list of scalar types as first argument to the ListBoxFor helper which will map to the creator ids that you want preselected:

public class IssueEditModel
{
    public IEnumerable<Creator> Creators { get; set; }
    public IEnumerable<int> SelectedCreatorIds { get; set; }
}

and then:

IssueEditModel issueEdit = new IssueEditModel 
{
    Creators = db.Creators,
    SelectedCreatorIds = db.Creators.Select(x => x.CreatorId)
};

and in the view:

<%: Html.ListBoxFor(
    x => x.SelectedCreatorIds, 
    new SelectList(
        Model.Creators, 
        "CreatorId", 
        "LastName"
    )
) %>

Upvotes: 3

Related Questions