Harry Tipper
Harry Tipper

Reputation: 95

Populate a drop down list with view model property: MVC 5, Razor

I've got a view with a few properties and want to create a drop down box for it. So far, I've created a list of selectListItems on my controller and added values to it and at the moment, it works and displays the items in the drop down list. However, my problem is that it's not linked at all to the property I have in my view model. How can I amend this?

controller:

            List<SelectListItem> jobs = new List<SelectListItem>();
        SelectListItem job1 = new SelectListItem() { Text = "Email", Value = "1", Selected = false };
        SelectListItem job2 = new SelectListItem() { Text = "OtherJob", Value = "2", Selected = false };
        SelectListItem job3 = new SelectListItem() { Text = "OtherJob2", Value = "3", Selected = false };
        SelectListItem job4 = new SelectListItem() { Text = "OtherJob3", Value = "4", Selected = false };
        SelectListItem job5 = new SelectListItem() { Text = "OtherJob4", Value = "5", Selected = false };
        SelectListItem job6 = new SelectListItem() { Text = "OtherJob5", Value = "6", Selected = false };
        SelectListItem job7 = new SelectListItem() { Text = "OtherJob6", Value = "7", Selected = false };
        SelectListItem job8 = new SelectListItem() { Text = "OtherJob7", Value = "8", Selected = false };
        jobs.Add(job1);
        jobs.Add(job2);
        jobs.Add(job3);
        jobs.Add(job4);
        jobs.Add(job5);
        jobs.Add(job6);
        jobs.Add(job7);
        jobs.Add(job8);
ViewBag.jobs = jobs;

        return View();

ViewModel:

public object Job { get; set; }

View:

    @using (Html.BeginForm(FormMethod.Post))
{
    <center>
        <div class="form-group">
            @Html.DropDownList("jobs", (IEnumerable <SelectListItem>)ViewBag.jobs)
        </div>
    </center>

Upvotes: 1

Views: 8750

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

Your name argument passed to DropDownList is wrong. It should be same as viewmodel property name to bind properly:

@Html.DropDownList("Job", (IEnumerable<SelectListItem>)ViewBag.jobs)

Since you have strongly-typed viewmodel, better to use DropDownListFor helper and change corresponding viewmodel property type to be same as SelectListItem.Value property type. Also you can eliminate cast from ViewBag usage by using a property which has type of IEnumerable<SelectListItem> and assign it directly to DropDownListFor:

Model

public class ViewModel
{
    public int Job { get; set; }

    public List<SelectListItem> Jobs { get; set; }
}

Controller

[HttpGet]
public ActionResult ControllerName()
{
    var model = new ViewModel();
    model.Jobs = new List<SelectListItem>();
    model.Jobs.Add(new SelectListItem() { Text = "Email", Value = "1", Selected = false });

    // assign other SelectListItem list values here

    return View(model);
}

View

@model ViewModel

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) 
{
    <div style="text-align:center">
        <div class="form-group">
            @Html.DropDownListFor(m => m.Job, Model.Jobs)
        </div>
    </div>
}

Upvotes: 4

Related Questions