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

为什么我不能在我的控制器中调用模型

如何解决为什么我不能在我的控制器中调用模型

我有一个问题,我不知道该怎么办。我尝试了几种方法,但对我没有任何作用。我有模特儿

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace BergClinic.Models
{
    public  class Patient
    {
        [Key]
        public int Id { get; set; }

        [required]
        [display(Name ="First Name")]
        [MaxLength(50)]
        public string FirstName { get; set; }

        [required]
        [display(Name = "Last Name")]
        [MaxLength(50)]
        public string LastName { get; set; }

        [required]
        [display(Name = "Date of Birth")]
        public DateTime DateOfBirth { get; set; }

        [display(Name = "Address")]
        [MaxLength(50)]
        public string Address { get; set; }

        [display(Name = "Phone Number")]
        public string PhoneNumber { get; set; }

        [display(Name = "Gender")]
        public PatientGender Gender { get; set; }
    }

    public enum PatientGender
    {
        [display(Name ="Male")]
        Male,[display(Name = "Female")]
        Female,UnkNown
    }
}

以及包含以下逻辑的 IPatientRepositoryPatientRepository

using BergClinic.DataAccess.Data;
using BergClinic.DataAccess.Repository.IRepository;
using BergClinic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BergClinic.DataAccess.Repository
{
    public class PatientRepository : Repository<Patient>,IPatientRepository
    {
        private readonly ApplicationDbContext _db;

        public PatientRepository(ApplicationDbContext db) : base(db)
        {
            _db = db;
        }

        public void Update(Patient patient)
        {
            var objDb = _db.Patients.FirstOrDefault(s => s.Id == patient.Id);
            if (objDb != null)
            {

                objDb.FirstName = patient.FirstName;
                objDb.LastName = patient.LastName;
                objDb.DateOfBirth = patient.DateOfBirth;
                objDb.Address = patient.Address;
                objDb.PhoneNumber = patient.PhoneNumber;
                objDb.Gender = patient.Gender;

                _db.SaveChanges();
            }
        }
    }
}

这里是包含更新方法IPatientRepository

using BergClinic.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace BergClinic.DataAccess.Repository.IRepository
{
    public interface IPatientRepository : IRepository<Patient>
    {
        void Update(Patient patient);
    }
}

一旦我在 PatientController 的管理区域中创建了它,我想初始化对象 Patient 但我不能。我想为 UpsertUpdate 创建 Insert Patient 方法,但是无论我输入什么它都看不到我的模型,只有它看到的是患者工作区

'Patient' is a namespace but is used like a type    


   using BergClinic.DataAccess.Repository.IRepository;
using Microsoft.AspNetCore.Mvc;
using BergClinic.Models;

namespace BergClinic.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class PatientController : Controller
    {
        private readonly IUnitOfWork _unitOfWork;

        public PatientController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Upsert(int? Id)
        {      
             Patient patient = new Areas.Patient
            return View();
        }


        #region API_CALLS
        [HttpGet]
        public IActionResult GetAll()
        {
            var patient = _unitOfWork.Patient.GetAll();
            return Json(new { data = patient });
        }

        #endregion
    }
}

只是注意到我使用 RepositoryPatten 和你的方式架构。我尝试重新启动 Visual Studio,但似乎什么也没发生,我尝试删除项目引用并再次尝试添加,但注意到再次发生。 这是我的 ProjectStructures 的几个场景,您可以查看:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?