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

How to Databind for MVVM Architecture

3 Answers 86 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 24 Jun 2011, 12:36 PM
Hi there,

I'm trying to bind controls in my webApplication to a ViewModel class.
The binding itself works fine, but i got a problem, resolving the correct objects.

I bound a RadGrid to an ObjectDataSource, refering my class "PersonsViewModel"
The select-method is
        public List<Person> GetPersons()
        {
            return DataProvider.GetPeople();
        }

Now I'd like to resolve the selectedItem for example:

        var items = RadGrid1.SelectedItems;
        Person p = items[0].DataItem as Person;

My problem is, that items[0].DataItem is always null.
I don't get why, because the item is databound.



Any help would be apprechiated.
Thanks,

Thomas

3 Answers, 1 is accepted

Sort by
0
Accepted
Sebastian
Telerik team
answered on 27 Jun 2011, 11:46 AM
Hello Thomas,

The DataItem object of the grid rows is not null only in a data-bound context, i.e. inside the ItemDataBound event handler of RadGrid (the same holds true for the standard MS GridView control).

If you want to extract the values from the fields of the underlying data object that is presented in a grid row in a different context/event, you can do that by fetching the data from the grid row cells as illustrated here. To get reference to the corresponding object of type Person, you may consider extracting the value of the key field in the grid item, traverse the Person list and fine the object that matches the value of this very key field.

Greetings,
Sebastian
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Thomas
Top achievements
Rank 1
answered on 27 Jun 2011, 12:18 PM
Fist of all, thanks for your fast answer.

If I got you right, then your advice would be, to resolve the corresponding object by myself?
But that would completly break with the concept of the databinding, wouldn't it?

I bind an object to a control, that works fine, but in my opinion, there must be a way,
it fetch the bound object, without resolving it manually, as shown in my code:

        var items = RadGrid1.SelectedItems;
        Person p = items[0].DataItem as Person;

I'd rather fetch the object, that was bound than building a new object by myself.

If I didn't get your reply the right way, it'd be nice, if you could provide me a codesnippet, that resolves my existing problem in the testsolution, attached to this thread.

Thanks a lot,
Thomas
0
Iana Tsolova
Telerik team
answered on 30 Jun 2011, 07:38 AM
Hello Thomas,

I find you properly understood my colleague. However I am not sure what are the issues you are facing when you try to implement it in your solution. Can you elaborate on the issue you are facing and your concerns?

Greetings,
Iana
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
General Discussions
Asked by
Thomas
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Thomas
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or