Добавил считывание с файла

parent d3e8bf8d
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Store.Models;
......@@ -7,45 +6,26 @@ namespace Store.Controllers
{
public class ProductController : Controller
{
public IActionResult Index()
{
List<Product> products = new List<Product>
{
new Product
{
Id = Guid.NewGuid().ToString(),
Name = "Asus A234",
Amount = 100,
Brand = "Asus",
Category = "TV"
},
new Product
private ProductContext _context;
public ProductController(ProductContext context)
{
Id = Guid.NewGuid().ToString(),
Name = "Asus A674",
Amount = 150,
Brand = "Asus",
Category = "TV"
_context = context;
}
};
ViewBag.products = products;
return View();
public IActionResult Index()
{
return View(_context.Products);
}
public IActionResult Details(string id)
{
return View(new Product
{
Id = Guid.NewGuid().ToString(),
Name = "Asus A674",
Amount = 150,
Brand = "Asus",
Category = "TV"
});
Product product = _context.Products.FirstOrDefault(p => p.Id == id);
ViewBag.product = product;
return View();
}
[ActionName("CreateProduct")]
[HttpGet]
public IActionResult Create()
{
......@@ -55,7 +35,8 @@ namespace Store.Controllers
[HttpPost]
public IActionResult Create(Product product)
{
_context.Products.Add(product);
_context.SaveChanges();
return View("Details", product);
}
}
......
@model Store.Models.Product
@{
Layout = "_MainLayout";
}
......@@ -8,22 +9,27 @@
<form action="/Product/Create" method="post" >
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" id="Name" name="Name" placeholder="Enter name">
<input type="text" class="form-control" value="@Model.Name" id="Name" name="Name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="">Category</label>
<input type="text" class="form-control" id="Category" name="Category" placeholder="Enter category">
<input type="text" class="form-control" value="@Model.Category" id="Category" name="Category" placeholder="Enter category">
</div>
<div class="form-group">
<label for="">Brand</label>
<input type="text" class="form-control" id="Brand" name="Brand" placeholder="Enter brand">
<input type="text" class="form-control" value="@Model.Brand" id="Brand" name="Brand" placeholder="Enter brand">
</div>
<div class="form-group">
<label for="">Amount</label>
<input type="text" class="form-control" id="Amount" name="Amount" placeholder="Enter amount">
<input type="text" class="form-control" value="@Model.Amount" id="Amount" name="Amount" placeholder="Enter amount">
</div>
<div class="form-group">
<label for="">Description</label>
<input type="text" class="form-control" value="@Model.Description" id="Description" name="Description" placeholder="Enter amount">
</div>
<button type="submit" class="btn btn-primary">Create</button>
......
@model Store.Models.Product
@{
Layout = "_MainLayout";
}
......@@ -9,19 +7,19 @@
<tbody>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
<td>@ViewBag.product.Name</td>
</tr>
<tr>
<td>Category:</td>
<td>@Model.Category</td>
<td>@ViewBag.product.Category</td>
</tr>
<tr>
<td>Brand:</td>
<td>@Model.Brand</td>
<td>@ViewBag.product.Brand</td>
</tr>
<tr>
<td>Amount:</td>
<td>@Model.Amount</td>
<td>@ViewBag.product.Amount</td>
</tr>
</tbody>
</table>
@using Store.Models
@model IEnumerable<Store.Models.Product>
@{
Layout = "_MainLayout";
}
......@@ -19,7 +19,7 @@
</tr>
</thead>
<tbody>
@foreach (Product product in ViewBag.products)
@foreach (Product product in Model)
{
<tr>
<th scope="row">@product.Id</th>
......
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