This is a migrated thread and some comments may be shown as answers.

ComboBoxFor binding to model object and not foreign key

1 Answer 279 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 20 Dec 2017, 12:53 AM

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!

1 Answer, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 22 Dec 2017, 11:51 AM
Hi Matt,

Attached you will find a small sample based on the snippets sent. You will notice, that after choosing an item from the ComboBox drop-down, the model.Shift.ShiftID property will be set to the selected item ID. That would be the expected result in the discussed scenario. Do you observe other behaviour at your end? If not, what would be the expected structure of the model, according to your requirements?

Regards,
Veselin Tsvetanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
ComboBox
Asked by
Matt
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or