0

I have this issue when I use my view page to create a patient:

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

I am not sure how to resolve my issue, I have been trying for hours. I am sure it is a simple fix.

Actually, I just realized that all my pages in the view when I try to run this comes up with the same error. What am I doing wrong?

 My Model:

 namespace PatientHut.Data.Models
 {
 public enum Gender 
 {
    Male, Female
 }


 public class Patient
 {
    public int Id { get; set; }

    public string Name { get; set; }

    public Gender Gender { get; set; }

    public DateTime DateOfBirth { get; set; }

    public string EmailAddress { get; set; }

    public string PhoneNumber { get; set; }

    public string Address { get; set; }

    public int Age => (DateTime.Now - DateOfBirth).Days / 365;


    //EF Relationship - A Patient can have many bookings 

    public IList<AppointmentBooking> Bookings { get; set; } = new 
    List<AppointmentBooking>(); 
  
  }
  }

   My services: 


     public Patient AddPatient(Patient u) ///!!!!!!!!!!!!!! 
        //an Id to check(its new).
     {
        var existing = GetPatientByEmail(u.EmailAddress); //check if user has same email 
        address when creating a new user 

        if (existing != null)
        {
            return null;

            //if no user the same is found return null
        }

        {
            var user = new Patient //create new user object 
            {
                Name = u.Name,
                Gender = u.Gender,
                DateOfBirth = u.DateOfBirth,
                EmailAddress = u.EmailAddress,
                PhoneNumber = u.PhoneNumber,
                Address = u.Address,
                

            //Add users details
            };

            db.Patients.Add(user); // Add to the DB
            db.SaveChanges(); //SaveChanges to the DB

            return user; // returning new user 

        }
     }


  My controller


    public Patient AddPatient(Patient u) ///!!!!!!!!!!!!!! 
        //an Id to check(its new).
    {
        var existing = GetPatientByEmail(u.EmailAddress); //check if user has same email 
    address when creating a new user 

        if (existing != null)
        {
            return null;

            //if no user the same is found return null
        }

        {
            var user = new Patient //create new user object 
            {
                Name = u.Name,
                Gender = u.Gender,
                DateOfBirth = u.DateOfBirth,
                EmailAddress = u.EmailAddress,
                PhoneNumber = u.PhoneNumber,
                Address = u.Address,
                

            //Add users details
            };

            db.Patients.Add(user); // Add to the DB
            db.SaveChanges(); //SaveChanges to the DB

            return user; // returning new user 

        }
    }


 My view

 @model PatientHut.Data.Models.Patient

   @{
 ViewData["Title"] = "Create";
 }

 <h1>Create</h1>

 <h4>Patient</h4>
 <hr />
 <div class="row">
 <div class="col-md-4">
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
     
 
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Gender" class="control-label"></label>
            <input asp-for="Gender" class="form-control" />
            <span asp-validation-for="Gender" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="DateOfBirth" class="control-label"></label>
            <input asp-for="DateOfBirth" class="form-control" />
            <span asp-validation-for="DateOfBirth" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="EmailAddress" class="control-label"></label>
            <input asp-for="EmailAddress" class="form-control" />
            <span asp-validation-for="EmailAddress" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="PhoneNumber" class="control-label"></label>
            <input asp-for="PhoneNumber" class="form-control" />
            <span asp-validation-for="PhoneNumber" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Address" class="control-label"></label>
            <input asp-for="Address" class="form-control" />
            <span asp-validation-for="Address" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>
 </div>
 </div>

 <div>
 <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
 @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
jps
  • 20,041
  • 15
  • 75
  • 79
user450157
  • 29
  • 3
  • 11

1 Answers1

2

You're handing an array into it. Try adding

.FirstOrDefault(); 

after wherever that error is being thrown. If you're not already, you'll need to add a "using System.Linq;" at the top as well.

I.E., if your code is throwing an error here(fake code, since I can't see where your error is being thrown from your code):

return patients;

Switch it to:

return patients.FirstOrDefault();
NicholaiRen
  • 196
  • 4
  • 15
  • thank you. However, I worked it out and I had two view pages that had the same information that was causing the problem! Thanks for this though! – user450157 Jul 16 '21 at 20:58