Добавил repository для модели Person

parent ea4d4d75
using Microsoft.AspNetCore.Mvc;
using MyCities.Repositories;
namespace MyCities.Controllers
{
public class PersonController : Controller
{
private IPersonRepository _repository;
public PersonController(IPersonRepository repository)
{
_repository = repository;
}
// GET
public IActionResult Index()
{
return View();
return View(_repository.Persons);
}
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public interface IPersonRepository
{
List<Person> Persons { get; }
void Add(Person person);
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public class InMemoryPersonRepository : IPersonRepository
{
private List<Person> _persons = new List<Person>();
public List<Person> Persons => _persons;
public void Add(Person person)
{
_persons.Add(person);
}
}
}
......@@ -34,6 +34,7 @@ namespace MyCities
});
services.AddSingleton<ICityRepository, InMemoryCityRepository>();
services.AddSingleton<IPersonRepository, InMemoryPersonRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
......
@model IEnumerable<MyCities.Models.City>
@model IEnumerable<MyCities.Models.Person>
@{ Layout = "_Layout";}
<table class="table small table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Counry</th>
<th>Population</th>
<th>FullName</th>
<th>NickName</th>
<th>Avatar</th>
</tr>
</thead>
<tbody>
@foreach (var city in Model)
@foreach (var person in Model)
{
<tr>
<td>@city.Name</td>
<td>@city.Country</td>
<td>@city.Population.ToString("#,###")</td>
<td>@person.FullName</td>
<td>@person.NickName</td>
<td><img src="@person.AvatarImagePath" width="250" height="250"/></td>
</tr>
}
</tbody>
</table>
<a href="/Home/Create" class="btn btn-primary">Create city</a>
<a href="/Person/Create" class="btn btn-primary">Create person</a>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment