簡體   English   中英

InvalidOperationException:傳入 ViewDataDictionary 的模型項的類型是“System.Collections.Generics - 如何解決這個問題?”

[英]InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generics - How to fix this?

當我使用我的視圖頁面創建患者時,我遇到了這個問題:

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

你將一個數組交給它。 嘗試添加

.FirstOrDefault(); 

在拋出該錯誤的任何地方之后。 如果您還沒有,您還需要在頂部添加一個“ using System.Linq; ”。

IE,如果您的代碼在此處拋出錯誤(假代碼,因為我看不到您的代碼從何處拋出錯誤):

return patients;

切換到:

return patients.FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM