Getting acces to List in model in ASP .Net Core MVC 2.1

asp.net c# entity-framework-core razor

Question

I want to do an simply ASP .Net Core MVC APP.

I have two models:

public class Farming
{
    [Key]
    public int    Id                 { get; set; }
    public string Localization       { get; set; }
    public List<Animal> Animals      { get; set; } //i want to get into it 
    public List<Worker> Workers      { get; set; } //from cshtml file
}

public class Animal 
{
    [Key]
    public int      Id                  { get; set; }
    public Species  Species             { get; set; }
    public DateTime BirthDate           { get; set; } 
    public bool     Sex                 { get; set; } 
    public DateTime ImpregnationDate    { get; set; } 
}

public class Worker 
{
    [Key]
    public int      Id                  { get; set; }
    public string   FirstName           { get; set; }
    public string   LastName            { get; set; } 
    public string   Position            { get; set; } 
    public float    Salary              { get; set; } 
    public DateTime EmploymentDate      { get; set; } 
    public DateTime EndOfEmploymentDate { get; set; } 
}

I setting up my database, it works ok. In dbo.Animals (dbo.Workers works too) in SQL Object explorer everything works pretty fine:

enter image description here

What i want to do, is to get acces into List<Animal> Animals and list of Workers fromIndex.cshtml page genereted internally in ASP .Net Core MVC in another List.cshtml file. Controller is genereted by Scaffolding too.

My Index.cshtml (i short it a little)

@model IEnumerable<Farm.Models.Farming>

@{
    ViewData["Title"] = "Index";
}
//foreach loop here with "item"
<a asp-action="List" asp-route-id="@item.Id">@Html.DisplayFor(modelItem => item.Localization)</a>

Method for going to List.cshtml from FarmingController.cs

public async Task<IActionResult> List(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
        var farming = await _context.Farms.FirstOrDefaultAsync(m => m.Id == id);
        if (farming == null)
        {
            return NotFound();
        }

        return View(farming);
    }

So, how to get acces into Models.Farming.Animal and Models.Farming.Worker from imported @SolutinName.Models.Farmin in List.cshtml?

EDIT1:

I tried to get acces to Animal in this way:

var animal = await _context.Animals.FirstOrDefaultAsync(a => a.FarmingId == id);
//i get `id` poperty from func `<IActionResult>` that i mentioned higher in this question

But i get information from Visual, that Animal dont have definition FarmingId (that is true, look at Animal model) but it exist in dbo.Animals. Im new in Core, any ideas?

EDIT2:

I edited database to this form, now it works better:

public class Farming
{
    [Key]
    public int    Id                      { get; set; }
    public string Localization            { get; set; }

    public List<Animal>      Animals      { get; set; }//
    public List<Worker>      Workers      { get; set; }//
}

public class Animal 
{
    [Key]
    public int      Id                       { get; set; }
    public DateTime BirthDate                { get; set; } 
    public bool     Sex                      { get; set; } 
    public DateTime ImpregnationDate         { get; set; } 

    [ForeignKey("Farming")]
    public int      FarmingId                { get; set; }
    public Farming  Farming                  { get; set; }
}

public class Worker //pracownik
{
    [Key]
    public int      Id                  { get; set; }
    public string   FirstName           { get; set; }
    public string   LastName            { get; set; } 
    public string   Position            { get; set; } 
    public float    Salary              { get; set; } 
    public DateTime EmploymentDate      { get; set; } 
    public DateTime EndOfEmploymentDate { get; set; } 

    [ForeignKey("Farming")]
    public int      FarmingId           { get; set; }
    public Farming  Farming             { get; set; }
}
1
0
12/7/2018 5:15:25 PM

Accepted Answer

Everything seems to be okay, however try to explicitly include the animals

 Farm theFarm = _context.Farms.Include(f => f.Animals).Where(f => f.Id == farmId);

the object theFarm will contain all fields of the farm and also the list with animals. and farmId is the id of the farm you want to get.

use this to get all farms with their animals:

var theFarms = _context.Farms.Include(f => f.Animals).ToList();
1
12/7/2018 2:56:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow