微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

InvalidOperationException:传入 ViewDataDictionary 的模型项的类型是“System.Collections.Generics - 如何解决这个问题?”

如何解决InvalidOperationException:传入 ViewDataDictionary 的模型项的类型是“System.Collections.Generics - 如何解决这个问题?”

我在使用视图页面创建患者时遇到此问题:

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

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

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

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

解决方法

您正在将一个数组传递给它。尝试添加

.FirstOrDefault(); 

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

即,如果您的代码在此处抛出错误(假代码,因为我看不到您的代码在哪里抛出错误):

return patients;

切换到:

return patients.FirstOrDefault();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。