В книге описан пример добавления категорий но у меня возникает в этом проблема. Нажимая именно на Categories должны выводится формочки но их нет.
public class CategoriesContoller : Controller
{
private ICategoryRepository repository;
public CategoriesContoller(ICategoryRepository repo)
{
repository = repo;
}
public IActionResult Index() => View(repository.Categories);
[HttpPost]
public IActionResult AddCategory(Category category)
{
repository.AddCategory(category);
return RedirectToAction(nameof(Index));
}
public IActionResult EditCategory(long id)
{
ViewBag.EditId = id;
return View("Index", repository.Categories);
}
[HttpPost]
public IActionResult UpdateCategory(Category category)
{
repository.UpdateCategory(category);
return RedirectToAction(nameof(Index));
}
[HttpPost]
public IActionResult DeleteCategory(Category category)
{
repository.DeleteCategory(category);
return RedirectToAction(nameof(Index));
}
}
public interface ICategoryRepository
{
IEnumerable<Category> Categories { get; }
void AddCategory(Category category);
void UpdateCategory(Category category);
void DeleteCategory(Category category);
}
public class CategoryRepository : ICategoryRepository
{
private DataContext context;
public CategoryRepository(DataContext ctx) => context = ctx;
public IEnumerable<Category> Categories => context.Categories;
public void AddCategory(Category category)
{
context.Categories.Add(category);
context.SaveChanges();
}
public void UpdateCategory(Category category)
{
context.Categories.Update(category);
context.SaveChanges();
}
public void DeleteCategory(Category category)
{
context.Categories.Remove(category);
context.SaveChanges();
}
}
@model Category
<div class="row p-2">
<div class="col-1"></div>
<div class="col">
<input asp-for="Name" class="form-control" />
</div>
<div class="col">
<input asp-for="Description" class="form-control" />
</div>
<div class="col-3">
@if (Model.Id == 0)
{
<button type="submit" class="btn btn-primary">Add</button>
}
else
{
<button type="submit" class="btn btn-outline-primary">Save</button>
<a asp-action="Index" class="btn btn-outline-secondary">Cancel</a>
}
</div>
</div
@model IEnumerable<Category>
<h3 class="p-2 bg-primary text-white text-center">Categories</h3>
<div class="container-fluid mt-3">
<div class="row">
<div class="col-1 font-weight-bold">Id</div>
<div class="col font-weight-bold">Name</div>
<div class="col font-weight-bold">Description</div>
<div class="col-3"></div>
</div>
@if(ViewBag.EditId == null)
{
<form asp-action="AddCategory" method="post">
@Html.Partial("CategoryEditor", new Category())
</form>
}
@foreach(Category c in Model)
{
@if(c.Id == ViewBag.EdiId)
{
<form asp-action="UpdateCategory" method="post">
<input type="hidden" name="Id" value="@c.Id"/>
@Html.Partial("CategoryEditor", c)
</form>
}
else
{
<div class="row p-2">
<div class="col-1">@c.Id</div>
<div class="col">@c.Name</div>
<div class="col">@c.Description</div>
<div class="col-3">
<form asp-action="DeleteCategory" method="post">
<input type="hidden" name="Id" value="@c.Id"/>
<a asp-action="EditCategory" asp-route-id="@c.Id" class="btn btn-outline-primary">Edit</a>
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
</div>
</div>
}
}
</div>