0

I am developing an asp mvc 5 website. I have 3 models which are appointments, consultations and contacts. now i want to display all the details of these 3 models in a view called userdetails. What i have done so far Created a view model

public class AllServices
{
    public List<Contact> Contacts { get; set; }
    public List<Appointment> Appointments { get; set; }
    public List<Consultation> Consultations { get; set; }
}

Then created the controller action method

public ActionResult userdetails()
{ 
    AllServices services = new AllServices();
    services.Appointments = dbcontext.Appointments.ToList();
    services.Consultations = dbcontext.Consultations.ToList();
    services.Contacts = dbcontext.Contacts.ToList();
    return View(services);
}

and in view i made

@model List<Astrology.ViewModels.AllServices>
....
@foreach(var item in Model)
{
    @item.Appointments
}

but when i run the page iam getting an error like this

The model item passed into the dictionary is of type 'Astrology.ViewModels.AllServices', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[Astrology.ViewModels.AllServices]'.

Can someone please help me with it. Iam stuck with this.

Abhijith
  • 71
  • 8
  • Because you pass a single instance of `AllServices` to a view which expects a collection of `AllServices`. Change the view to `@model Astrology.ViewModels.AllServices`. And its `@foreach(var item in Model.Appointments) { ...` –  Aug 13 '17 at 13:27
  • what if i want to display all data one model by model in a table with iterations. – Abhijith Aug 13 '17 at 13:33
  • Read the 1st comment! –  Aug 13 '17 at 13:34
  • I got this output in view when i changed view like u said.. this time no error but the object name itself System.Collections.Generic.List`1[Astrology.Models.Appointment] – Abhijith Aug 13 '17 at 13:37
  • FGS. `@foreach(var item in Model.Appointments) {` is iterating through your collection of `Appointments` - you need to display **properties** of each `Appointment` - `@item.somePropertyOfYourAppointmentModel` –  Aug 13 '17 at 13:39
  • I have changed view as @model Astrology.ViewModels.AllServices *newline* "@Model.Appointments" how can i iterate when im getting only single services object. please tell me how to get all data from that 3 models in my view – Abhijith Aug 13 '17 at 13:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151827/discussion-between-stephen-muecke-and-abhijith). –  Aug 13 '17 at 13:43

1 Answers1

0

The error is telling you that you're only passing a single AllServices model when it expects a List of AllServices models. You need to either change the view to expect one model or put the single model into a list.

EDIT (adding code sample) Change your Controller to something like this:

public ActionResult userdetails()
{ 
    List<AllServices> lservices = new List<AllServices>();
    AllServices services = new AllServices();
    services.Appointments = dbcontext.Appointments.ToList();
    services.Consultations = dbcontext.Consultations.ToList();
    services.Contacts = dbcontext.Contacts.ToList();
    lservices.Add(services);
    return View(lservices);
}
Scott Dellinger
  • 158
  • 1
  • 12