0

当我使用我的视图页面创建患者时,我遇到了这个问题:

InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“System.Collections.Generic.List`1[PatientHut.Data.Models.Patient]”,但此 ViewDataDictionary 实例需要“PatientHut.Data.Models”类型的模型项。病人'。

我不知道如何解决我的问题,我已经尝试了几个小时。我相信这是一个简单的修复。

实际上,我刚刚意识到,当我尝试运行它时,视图中的所有页面都会出现相同的错误。我究竟做错了什么?

 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");}
}
4

1 回答 1

2

您正在将一个数组放入其中。尝试添加

.FirstOrDefault(); 

在抛出该错误的任何地方之后。如果您还没有,您还需要using System.Linq;在顶部添加一个“”。

IE,如果你的代码在这里抛出错误(假代码,因为我看不到你的代码从哪里抛出错误):

return patients;

将其切换为:

return patients.FirstOrDefault();
于 2021-07-16T18:36:34.213 回答