Witam mam problem z wyświetleniem zdjęć w widoku listy produktów.
W encji Product zdjęcie określa zmienna public string Image{get ; set;}.Gdy dodaje nowy produkt do bazy,wybieram zdjęcie z komputera.Nazwa zdjęcia zapisywana jest w bazie,a w
w widoku index wyświetla mi po prostu biały kwadracik :D
Może coś z tym .first() jest nie tak,ale nie mogłem inaczej bo mam tutaj IPageList i mi krzyczy :
PagedList.IPagedList<Hurtownia.Models.Product>' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'PagedList.IPagedList<Hurtownia.Models.Product>' could be found (are you missing a using directive or an assembly reference?
ProductController metoda Create.
[Authorize]
public ActionResult Create()
{
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
return View();
}
// POST: /Product/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Description,Name,Price,Quantity,CategoryId")] Product product)
{
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
try
{
HttpPostedFileBase file = Request.Files["imageFile"];
if (file != null && file.ContentLength > 0)
{
product.Image = file.FileName;
file.SaveAs(HttpContext.Server.MapPath("../Images/") + product.Image);
}
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
return View(product);
}
}
Cześć widoku Create
@model Hurtownia.Models.Product
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" })
<div class="editor-field">
<input type="file" name="imageFile" size="23" accept="gif|jpg|png"/>
@Html.ValidationMessageFor(model=>model.Image)
</div>
</div>
Część widoku index(lista produktow)
@model PagedList.IPagedList<Hurtownia.Models.Product>
@using PagedList.Mvc;
@using PagedList;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Products";
}
<div class="page-header">
<h2>Products</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
</div>
@using (Html.BeginForm("Index", "Product", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
Image
</th>
<th>
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Description", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Price
</th>
@if (User.IsInRole("Admin"))
{
<th>
Ilosc
</th>
}
<th>
Category
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<div class="display-label">Image</div>
<div class="display-field">
@Html.DisplayFor(model => item.Image)
<img src="../../Images/@Model.First().Image" alt="" height="100px" width="100px" />
</div>