ListBoxFor in .NET Core not binding

.net-core entity-framework-core


I cannot get this ListBox to have a value. It worked in MVC4.5, from which I am translating; cannot get to work in MVC Core. Swear it was working at some point; now it is not.

Have tried: etc.


        <div class="tb-field desc-field">
            @Html.ListBoxFor(model => model.workerRequests,
                new SelectList(ViewBag.workerRequests, "Value", "Text"),
                    new { id = "workerRequests2_WO-"+ Model.ID, tabindex = "22", style="min-width: 16em;"}

Controller (GET)

            wo.workerRequests = new List<ViewModel.WorkerRequest>();
            ViewData["workerRequests"] = new SelectList(wo.workerRequests);
            return PartialView("Create", wo);

Controller (POST)

        public async Task<ActionResult> Create(WorkOrder wo, string userName)
            var modelUpdated = await _adaptor.TryUpdateModelAsync(this, wo);
            // ... "wo.WorkerRequests" is empty here ^


        public virtual ICollection<WorkerRequest> workerRequests { get; set; }

Additional info:

Apparently it is coming across in the Request, but as a StringValue:


...apparently all of them are string values. It's not getting bound to its desired type.


  1. It has getters and setters on the entity.
  2. I'm populating the ViewData before serving the page.
  3. I'm using a SelectList(arg) where arg is of type myModel.myProperty.

We are selecting the Worker in the WorkerRequest from the database (nested type). Is this due to the lazy-loading bugs? What am I doing wrong?

11/25/2019 7:15:53 PM

Accepted Answer

It looks like you are creating 2 SelectList objects for the view.

Once in controller:

ViewData["workerRequests"] = new SelectList(wo.workerRequests);

And again in the view:

@Html.ListBoxFor(model => model.workerRequests,
    new SelectList(ViewBag.workerRequests, "Value", "Text"),

You only need to create one and the best place would be in the view. So change your controller code to populate the ViewData with the data and let the view create the SelectList to display:

ViewData["workerRequests"] = wo.workerRequests;

Now you should see the list box populated with the data.

7/14/2019 3:36:18 AM

Popular Answer

For the curious, this is what we ended up with:


        public ActionResult Create(Domain.WorkOrder wo, string userName, List<Domain.WorkerRequest> workerRequestList)
            Domain.WorkOrder neworder = woServ.Create(wo, userName);           

            // JSON object with new work order data
            var result = map.Map<Domain.WorkOrder, ViewModel.WorkOrder>(neworder);
            return Json(new 
                sNewRef = result.tabref,
                sNewLabel = result.tablabel,
                iNewID = result.ID


        <div class="tb-label desc-label">
            @Html.LabelFor(model => model.workerRequests)
            <br />
            <input type="button" value="@Machete.Web.Resources.WorkOrder.requestAdd" class="formButton" id="addRequestBtn-@(Model.ID)"/>
            <input type="button" value="@Machete.Web.Resources.WorkOrder.requestRemove" class="formButton" id="removeRequestBtn-@(Model.ID)"/>
        <div class="tb-field desc-field">
            @Html.ListBox("workerRequests2", new SelectList(ViewBag.workerRequests, "Value", "Text"), new { id = "workerRequests2_WO-"+ Model.ID, tabindex = "22", style="min-width: 16em;"})

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow