I am creating a data-entry page on an ASP.NET MVC Core application using EF Core.
I am using ViewData to populate the ComboBox.
public IActionResult Add()
{
ViewData["CostCenterID"] = _context.CostCenters.ToList();
ViewData["ReasonCodeID"] = _context.ReasonCodes.ToList();
ViewData["ShiftID"] = _context.Shifts.ToList();
return View();
}
The razor view for one of the combo boxes looks like this:
<div class="form-group">
@Html.LabelFor(model => model.Shift, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().ComboBoxFor(model => model.Shift.ShiftID)
.HtmlAttributes(new { @class = "form-control", @style = "width: 35%;" })
.Placeholder("Select a shift...")
.DataValueField("ShiftID")
.DataTextField("Name")
.BindTo((System.Collections.IEnumerable)ViewBag.ShiftID)
)
<span asp-validation-for="Shift" class="text-danger"></span>
</div>
</div>
When I save the form, the Shift property is set and the ShiftID property is still 0. There are a total of 5 combo boxes on this form. One is used just to cascading another combo box. That combo box sets the Entity property as well, and that two needs the ID set. I figure once one gets solved the rest will fall in place. If I use regular select objects with asp-for properties it works, but I want to use the Telerik controls.
The save method looks like this:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add(DownTimeEntry downTimeEntry)
{
if (ModelState.IsValid)
{
_context.Add(downTimeEntry);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CostCenterID"] = _context.CostCenters.ToList();
ViewData["ReasonCodeID"] = _context.ReasonCodes.ToList();
ViewData["ShiftID"] = _context.Shifts.ToList();
return View(downTimeEntry);
}
Any help on this would be appreciated!