Mr. P
Mr. P

Reputation: 103

How do I pass a list of objects from a WCF service to an ASP.NET web app

I am trying to pass a list from my WCF service to my ASP.NET web app.

First I passed a usual list of objects and used System.Collections.Generic.List in my configuration, everything worked out just fine.

Now I changed my code and made an object out of my list.
The class looks like this:

public class Persons : List<Person>
{
    private List<Person> personlist;

    public Persons()
    {
        personlist = new List<Person>();
    }
}

Now I struggle with the configurations.
What do I have to change in my configurations?
I set the collections type to: System.Collections.ObjectModel.Collection
and my client isn't giving me an error:

WCF.Persons personlist = client.GetPersonList(@"C:\example");

But when I run the application I get an error:

The model item passed into the dictionary is of type 'WCFasp.net.WCF.Persons', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WCFasp.net.WCF.Person]'.

Is it even possible to pass such a thing? And where do I have to make the changes?

I searched a lot for a good answer but couldn't find anything helpful.

Upvotes: 0

Views: 773

Answers (2)

Siva Kumar Siddam
Siva Kumar Siddam

Reputation: 154

WCF.Persons persons = client.GetPersonList(@"C:\example"); 

return View(persons);

your view should be as follows

@model WCF.Persons

@foreach(var person in Model)
{ 
@person
} 

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Your wcf client code is fine, the problem is in the view, as it only understands object of type List<Person>, you can up cast it to Base type which is List<Person> that before passing it to view like:

WCF.Persons personlist = client.GetPersonList(@"C:\example"); 

return View(personlist as List<Person>);

and your View is strongly typed to List<Person> i suspect :

@model List<Person> 

Upvotes: 2

Related Questions