I have hard time uploading picture and then displaying it in my view. I went through a lot of posts here and for some reason nothing really works for me.
Picture gets uploaded (in my case saved in ~/Content/Images, but is not displayed in the view. Whole project is published on Azure.
Please abstract from things like verifying if the uploaded file is really a picture or handling the size of the picture.
My domain model is:
public class Product
{
...
public string ProductImageUrl { get; set; }
...
}
Controller - post method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(/*[Bind(Include = "ProductId,ProductName,ProductDescription,ProductPrice, CategoryId, Quantity, picture")]*/ Product product, HttpPostedFileBase picture)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
if (picture != null)
{
var originalFileName = Path.GetFileName(picture.FileName);
string fileId = Guid.NewGuid().ToString().Replace("-", "");
var path = Path.Combine(Server.MapPath("~/Content/Images/"), fileId + ".jpg");
picture.SaveAs(path);
product.ProductImageUrl = path;
product.ProductImageName = fileId;
db.SaveChanges();
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
My view is:
@foreach (var item in Model)
{
<tr>
<td>
<img src="@item.ProductImageUrl" />
</td>
...
item.ProductImageUrl?