I have a Manager class, which has a property of type Region... both are objects persisted to a database using OpenAccess.
When creating a manager, I'd like a combo to select their region, populated with a list of available regions.
So far I have:-
@model Manager...<div> @(Html.Telerik().ComboBoxFor(o => o.Region)
.Name("RegionCombo") .BindTo(ViewData["regions"] as SelectList) )</div>With the region list populated something like this in the controller:-
public ActionResult Create(){ ViewData["regions"] = new SelectList(meta.GetRegionList(), "ID", "Name"); return View();}So far so good... I get a form with a combo that has all my regions in it. However on post back, the Region property of my Manager object is null (other string properties etc are OK).
I read somewhere in the forum that the Name property breaks it... however if I remove that, ModelState.IsValid returns false.
I know I've got the syntax wrong here, but I can't find a good example of getting the selected value.
Do I need to write an intermediary model between the view and my business object? i.e. one that takes Region as string then translates into an object in the controller?
Any help greatly appreciated...
7 Answers, 1 is accepted
This happens because you have overridden the name of the combobox. ASP.NET MVC model binding relies on the name to map posted values to object properties. Try not to set the Name explicitly as ComboBoxFor generates one automatically (much like TextBoxFor or DropDownListFor).
Kind regards,Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
<div> @(Html.Telerik().ComboBoxFor(o => o.Region) .BindTo(ViewData["regions"] as SelectList) )</div>[HttpPost]public ActionResult Create(Manager manager){ try { if (ModelState.IsValid) { MetaService.MetaClient meta = new MetaService.MetaClient(); meta.AddManager(manager); } else { Debug.WriteLine("Invalid ModelState"); } return RedirectToAction("Index"); } catch { return View(); }}Even if I break in the function and inspect the "manager" instance... the Region property is still null (other properties are OK)
Does this work with Html.DropDownListFor? If it does then there may be a problem with the Telerik combobox. However if it fails even with Html.DropDownListFor the problem is elsewhere.
Regards,Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
I've gotten a bit further an discovered I need to use o.Region.ID instead of o.Region, and MVC is meant to handle the conversion from the string value to the strongly typed property.
Now when I submit, the property is set, but with a new (empty) instance of the Region class (with only the ID set)... not the existing region for which the selected ID represents.
So when I try to persist my new Manager object, OpenAccess is trying to create a new Region as well.
In this situation I want it to reference the existing Region entity (based on the selected item from the Combo) not create a new instance of a Region and set the ID property.
I know this might be more appropriate in the OA forum now, but I'm hoping someone could point out if it's at least *meant* to work the way I'm trying...
ASP.NET MVC is not aware of the underlying business logic. It sees a complex property and instantiates a new object using the posted data. You need to make sure the right region object is used as ASP.NET MVC will not go to the database and create it for you. Here is how to do so:
TryUpdateModel(manager);manager.Region = GetRegionById(manager.Region.ID);I hope this helps, Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!