How to return a compound field in a dropdown by ASP CORE using Razor

Asked

Viewed 10 times

-1

original in the model = ViewData["JobId"] = new SelectList(_context.Job, "JobId", "JobDay"); Together with Jobday, put the field Job.Entity.Entityname, Job and Entity are related and in context. front end: <select asp-for="Team.JobId" class ="form-control" asp-items="ViewBag.JobId"></select>

1 answer

0


Capturing data:

var ListJobs = from j in _context.Job
                            .Include(k => k.Entity)
                            .OrderBy(k => k.Entity.EntityName)
                             select j;

Creating the View Controler: ViewData["JobId"] = new SelectList(ListJobs, "JobId", "JobDay", "Entity.EntityName", "Entity.EntityName");

Creating the Client View:

<div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Team.JobId" class="control-label"></label>
                <select asp-for="Team.JobId" class="form-control" asp-items="ViewBag.JobId"></select>
            </div>

Final Result: Grouped DropDown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.