Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Model navigation property returns null value in my asp.net core project.

$
0
0

Hi

I have some entities like Patients with some relations to another entities. My problem is that, in Details view, the navigation properties (which points to other tables) does not display anything! apparently it returns null.

after a lot of search, some people says i should change some configation options in my dbContext (see this link), but i could not find Configuration property!

some people says Lazy loading is not supported by ef core, i should include it manually in my linq query (see this link), but also i could not find Include in my dbContext!!

Here is my Patients class :

public partial class Patients
{
        [Key]
        public Guid PatientRowID { get; set; }
        [Required]
        [StringLength(50)]
        public string PatientCaseID { get; set; }
        [StringLength(50)]
        public string PatientFname { get; set; }
        [StringLength(50)]
        public string PatientLname { get; set; }
	public int? SexID { get; set; }
	[ForeignKey(nameof(SexID))]
        [InverseProperty("Patients")]
        public virtual Sex Sex { get; set; }
}

And here is my another table (Sex) :

public partial class Sex
{
        public Sex()
        {
            Patients = new HashSet<Patients>();
            Persons = new HashSet<Persons>();
        }

        [Key]
        public int SexID { get; set; }
        [StringLength(50)]
        public string SexName { get; set; }

        [InverseProperty("Sex")]
        public virtual ICollection<Patients> Patients { get; set; }
        [InverseProperty("Sex")]
        public virtual ICollection<Persons> Persons { get; set; }
}

And here is my action method :

public IActionResult Details(string id)
        {
            if (string.IsNullOrEmpty(id))
                return BadRequest();

            Guid gid = Guid.Parse(id);
            var patient = _dbContext.Patients.Where(p => p.PatientRowID == gid).FirstOrDefault();
            if (patient == null)
                return NotFound();

            return View(patient);
        }

And here is my Details view code :

<div><dl class="row"><dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.PatientCaseID)</dt><dd class = "col-sm-10">
            @Html.DisplayFor(model => model.PatientCaseID)</dd><dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.PatientFname)</dt><dd class = "col-sm-10">
            @Html.DisplayFor(model => model.PatientFname)</dd><dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.PatientLname)</dt><dd class = "col-sm-10">
            @Html.DisplayFor(model => model.PatientLname)</dd><dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.SexID)</dt><dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Sex.SexName) // display nothing!</dd></dl></div>

Can anybody help me where is my problem & how to solve it?

Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>