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

耗尽温柔 提交于 2019-12-11 15:50:03

问题


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>

回答1:


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


来源:https://stackoverflow.com/questions/50065702/populate-a-drop-down-list-with-view-model-property-mvc-5-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!