Cory
Cory

Reputation: 658

MVC 4 DropDownListFor Set Selected Item Based on Text

Normally when I use the DropDownListFor helper I'm selecting which item is selected based on an ID (int), but now I have a situation where I need to display which item is selected based on the text (string) and not an ID. In the controller the model is being set correctly to the value that I want with this property:

model.Title

An example title would be "Front Office". I have the following code in my Controller:

ViewBag.Titles = new SelectList(jobTitles, "Name", "Name");

and on the view I have this:

@Html.DropDownListFor(model => model.Title, ViewBag.Titles as SelectList)

The DropDownList is populating correctly with all the expected job titles, it just isn't selecting the correct job title based on model.Title. What am I doing wrong?

UPDATE:

There seems to be something else potentially going wrong in my code, so I'm putting all of it here to see if I'm doing something wrong.

Controller:

public ActionResult Edit(int id = 0)
    {
        StaffMember staffmember = StaffMember.SelectByID(id); // gets staff member from db

        ViewBag.Titles = new SelectList(JobTitle.SelectAll(), "Name", "Name", staffmember.Title); // JobTitle.SelectAll() returns List<JobTitle>

        StaffEditModel model = new StaffEditModel();
        model.ID = staffmember.ID;
        model.ClientID = staffmember.ClientID;
        model.FirstName = staffmember.FirstName;
        model.MiddleInitial = staffmember.MiddleInitial;
        model.LastName = staffmember.LastName;
        model.Title = staffmember.Title;
        model.Phone = staffmember.Phone;
        model.Email = staffmember.Email;
        model.Birthday = staffmember.Birthday;
        model.HireDate = staffmember.HireDate;
        model.Biography = staffmember.Biography;

        if (staffmember == null)
        {
            return HttpNotFound();
        }
        return View(model);
    }

Model:

public class StaffEditModel
{
    public int ID { get; set; }

    public int ClientID { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Middle Initial")]
    public string MiddleInitial { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    public string Title { get; set; } // this is the property I'm trying to show in DropDown

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public string Birthday { get; set; }

    [Display(Name = "Hire Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public string HireDate { get; set; }

    public string Email { get; set; }

    [MaxLength(14)]
    public string Phone { get; set; }
    public string Biography { get; set; }
}

View:

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.Titles,"Value","Text", Model.Title))

Upvotes: 1

Views: 16019

Answers (2)

James
James

Reputation: 435

I know I'm late to the party on this, but I had a similar problem and thought I would add what I've found.

I haven't figured out the why yet - I'm still researching (that's how I ended up here) - but I think it has something to do with the fact that you're using the word Title

I've got the exact same scenario and as soon as I changed my model to CustomerTitle instead of Title things worked as expected.

Initial thoughts are that the model binder is finding Title somewhere else in the DOM and getting tripped up. Complete speculation. If I have more time I'll keep digging for the why

Upvotes: 1

Paritosh
Paritosh

Reputation: 11560

This should work. You can pass ViewBag.Titles as List<SelectListItem> from controller

var jobList = new List<SelectListItem>();
foreach (var job in jobTitles)
{
    var item = new SelectListItem();
    item.Value = job.PropertyName; //the property you want to display i.e. Title
    item.Text = job.PropertyName;
    jobList.Add(item);
}
ViewBag.Title = jobList;

and then in the view,

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.Titles,"Value","Text", Model.Title))

Upvotes: 3

Related Questions