简体   繁体   中英

Two models in one view in ASP MVC

I'm having trouble implementing two models in a single View.

Here are my two models:

    public class DoctorViewModel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int GodineIskustva { get; set; }
    }
}

    public class PatientViewModel
    {
        public int Id { get; set; }   
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PersonalID { get; set; }
        public int DoctorId { get; set; }

    }
}

This is how I recieve a single model inside my View

@model IEnumerable<PatientViewModel>

I need to be able to loop through list of both doctors and patients inside my view.

My doctor service method for getting all doctors (as an exampple, same thing with patients)

public IEnumerable<Doctor> GetDoctors()
{
    return doctorRepository.GetAll();
}

And my controller class for patients (as an exampple, same thing with doctors)

public IActionResult Index()
        {

            List<PatientViewModel> model = new List<PatientViewModel>();
            patientService.GetUsers().ToList().ForEach(p =>
            {
                PatientViewModel patient = new PatientViewModel
                {
                    Id = p.Id,
                    FirstName = p.FirstName,
                    LastName = p.LastName,
                    PersonalID = p.PersonalID,
                    DoctorId = p.DoctorId
                };
                model.Add(patient);
            });

            return View(model);
        }

I have tried using tuples or a complex model class containing those two models and I could never get it to work. Does anyone have a suggestion how to do this?

My ViewModel class:

    public class DoctorPatientViewModel
    {
        public IEnumerable<Doctor> Doctors { get; set; }
        public IEnumerable<Patient> Patients { get; set; }
    }
}

And I get the following error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[WebApp.Models.PatientViewModel]', but this ViewDataDictionary instance requires a model item of type 'WebApp.Models.DoctorPatientViewModel'.

My GenericRepository class

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using ServiceLayer;
using DomainLayer.Entities;

namespace RepositoryLayer
{
    public class GenericRepository<T> : IGenericRepository<T> where T : SharedEntity
    {
        private readonly EntityContext context;
        private DbSet<T> entities;

        public GenericRepository(EntityContext context)
        {
            this.context = context;
            entities = context.Set<T>();
        }
        public IEnumerable<T> GetAll()
        {
            return entities.AsEnumerable();
        }

        public T Get(long id)
        {
            return entities.SingleOrDefault(s => s.Id == id);
        }
        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Add(entity);
            context.SaveChanges();
        }

        public void Update(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            context.SaveChanges();
        }

        public void Delete(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
            context.SaveChanges();
        }
        public void Remove(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
        }

        public void SaveChanges()
        {
            context.SaveChanges();
        }
    }
}

i faced this same issue,I got solution regarding this,Ienumerable is OK,i Have used List

now need to change in your View

@model 'Namespace'.'ViewModelsFolder'.DoctorPatientViewModel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM