Commit c9b66f17 authored by TTrueBenji's avatar TTrueBenji

Добавить функционал редактирования.

***
- реализовал редактирование смартвона.
- вынес форму редактирования и создания смартфона в PartialView.
***
parent 320b1ea3
......@@ -26,45 +26,6 @@ namespace PhoneStore.Controllers
return View();
}
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return PhysicalFile(filePath, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// byte[] bytes = System.IO.File.ReadAllBytes(filePath);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(bytes, fileType, fileName);
// }
// [HttpGet]
// public IActionResult Test()
// {
// string filePath = Path.Combine(_environment.ContentRootPath, "Files/test.pdf");
// FileStream stream = new FileStream(filePath, FileMode.Open);
// string fileType = "application/pdf";
// string fileName = "test.pdf";
// return File(stream, fileType, fileName);
// }
[HttpGet]
public IActionResult Test()
{
......@@ -77,7 +38,7 @@ namespace PhoneStore.Controllers
[ActionName("Test2")]
public string Test(Phone phone)
{
return "tets";
return null;
}
[HttpGet]
......
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PhoneStore.Models;
namespace PhoneStore.Controllers
{
public class PhoneController : Controller
public class PhonesController : Controller
{
private readonly MobileContext _db;
public PhoneController(MobileContext db)
public PhonesController(MobileContext db)
{
_db = db;
}
......@@ -22,13 +22,13 @@ namespace PhoneStore.Controllers
public IActionResult Index()
{
List<Phone> phones = _db.Phones.ToList();
return View(phones);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
......@@ -39,5 +39,29 @@ namespace PhoneStore.Controllers
_db.SaveChanges();
return RedirectToAction("Index");
}
[HttpGet]
public IActionResult Edit(int phoneId)
{
var phone = _db.Phones.FirstOrDefault(p => p.Id == phoneId);
if (phone is null)
{
return BadRequest();
}
return View(phone);
}
[HttpPost]
public IActionResult Edit(Phone phone)
{
if (phone is null)
{
return BadRequest();
}
_db.Phones.Update(phone);
_db.SaveChanges();
return RedirectToAction("Index");
}
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Pages" />
<Folder Include="Phones" />
</ItemGroup>
</Project>
......@@ -60,7 +60,6 @@ namespace PhoneStore
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
......
......@@ -8,20 +8,11 @@
<h2>Заполните форму для добавления устройства</h2>
<div class="row">
<div class="phone_add_form col-md-6">
<form asp-action="Create" asp-controller="Phone" method="post">
<form asp-action="Create" asp-controller="Phones" method="post">
<div class="form_row">
<label for="name">
Наименование
<input id="name" type="text" asp-for="Name">
</label>
<label for="company">
Производитель
<input id="company" type="text" asp-for="Company">
</label>
<label for="price">
Стоимость
<input id="price" type="text" asp-for="Price">
</label>
@{
await Html.RenderPartialAsync("PartialViews/PhoneFormPartialView");
}
<button class="btn btn-outline-warning">Отправить</button>
</div>
</form>
......
@model Phone
@{
ViewBag.Title = "Редактирование";
Layout = "_Layout";
}
<div class="row">
<div class="col-md-6">
<form asp-action="Edit" method="post">
@{
await Html.RenderPartialAsync("PartialViews/PhoneFormPartialView");
}
<input type="text" hidden asp-for="Id">
<button type="submit"
class="btn btn-outline-warning">Изменить</button>
</form>
</div>
</div>
@model List<Phone>
@{
ViewBag.Title = "title";
ViewBag.Title = @"title";
Layout = "_Layout";
}
<a asp-action="Create" asp-controller="Phone">Добавить устройство</a>
@* https://localhost:5001/Phone/Create method GET*@
<a asp-action="Create" asp-controller="Phones">Добавить устройство</a>
@if (Model.Count == 0)
{
<h2>Список пуст.</h2>
......@@ -46,8 +44,17 @@ else
Заказать
</a>
</td>
<td>
<a asp-route-phoneId="@phone.Id"
asp-action="Edit"
asp-controller="Phones"
class="btn btn-outline-warning">
Изменить
</a>
</td>
</tr>
}
</table>
}
@model Phone
<label for="name">
Наименование
<input id="name" type="text" asp-for="Name">
</label>
<label for="company">
Производитель
<input id="company" type="text" asp-for="Company">
</label>
<label for="price">
Стоимость
<input id="price" type="text" asp-for="Price">
</label>
\ No newline at end of file
......@@ -25,7 +25,7 @@
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Phone" asp-action="Index">Смартфоны</a>
<a class="nav-link text-dark" asp-area="" asp-controller="Phones" asp-action="Index">Смартфоны</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Orders" asp-action="Index">Заказы</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