Я хочу сделать простое приложение ASP .Net Core MVC.
У меня есть две модели:
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; }
}
Я настраиваю свою базу данных, она работает нормально.В dbo.Animals
(dbo.Workers works too)
в SQL Object Explorer все работает довольно хорошо:

Что я хочу сделать, это получить доступ в List<Animal> Animals
и список Workers
со страницы Index.cshtml
, сгенерированный внутренне в ASP .Net Core MVC, в другом файле List.cshtml
. Контроллер также генерируется Scaffolding.
My Index.cshtml
(я коротконемного)
@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>
Способ перехода на List.cshtml
с 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);
}
Итак, как получить доступ к Models.Farming.Animal
и Models.Farming.Worker
из импортированных @SolutinName.Models.Farmin
в List.cshtml
?
РЕДАКТИРОВАТЬ1:
Я пытался получить доступ к Animal таким образом:
var animal = await _context.Animals.FirstOrDefaultAsync(a => a.FarmingId == id);
//i get `id` poperty from func `<IActionResult>` that i mentioned higher in this question
Но я получаю информацию отВизуально, что Animal
не имеет определения FarmingId
(это правда, посмотрите на Animal
модель), но оно существует в dbo.Animals
. Я новичок в Core, есть идеи?
EDIT2:
Я отредактировал базу данных в этой форме, теперь она работает лучше:
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; }
}