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

[Solved] ComboBoxFor and Binding to Business Object

7 Answers 165 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
IT
Top achievements
Rank 1
IT asked on 15 Jul 2011, 06:32 AM
I'm hoping someone can give me a quick pointer on binding a ComboBox to a business object...

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

Sort by
0
Atanas Korchev
Telerik team
answered on 15 Jul 2011, 07:19 AM
Hello Aleks,

 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!

0
IT
Top achievements
Rank 1
answered on 15 Jul 2011, 07:27 AM
Thanks... but as mentioned I tried removing the Name property... the result is that I get an invalid ModelState

<div>
    @(Html.Telerik().ComboBoxFor(o => o.Region)
        .BindTo(ViewData["regions"] as SelectList)
    )
</div>

0
IT
Top achievements
Rank 1
answered on 15 Jul 2011, 07:38 AM
To clarify... this is what the post back looks like...

[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)
0
Atanas Korchev
Telerik team
answered on 15 Jul 2011, 07:57 AM
Hello Aleks,

 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!

0
IT
Top achievements
Rank 1
answered on 15 Jul 2011, 09:22 AM
OK... same thing happens with DropDownListFor, so it's not a ComboBox error... however...

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...
0
Atanas Korchev
Telerik team
answered on 15 Jul 2011, 09:27 AM
Hello Aleks,

 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!

0
IT
Top achievements
Rank 1
answered on 15 Jul 2011, 09:33 AM
That got it... many thanks for your help!
Tags
ComboBox
Asked by
IT
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
IT
Top achievements
Rank 1
Share this question
or