I can upload image files using mvc 4 razor. Now I want to show/view those images. I am using images for products. So, I want to show images for particular product. How can I show images related to that product?
Here is my Model:
namespace Online_Shopping_Management.Models
{
public class Product
{
[Key]
public int ProductId { set; get; }
public int? CategoryId { set; get; }
public virtual Category Category { set; get; }
public int? SubCategoryId { set; get; }
public virtual SubCategory SubCategory { set; get; }
public int? ModelId { set; get; }
public virtual Model Model { set; get; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
}
Here is my controller part:
public ActionResult Create(Product product)
{
foreach (var file in product.Files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
file.SaveAs(path);
}
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", product.CategoryId);
ViewBag.SubCategoryId = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryName", product.SubCategoryId);
ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName", product.ModelId);
return View(product);
}
Here is my view part:
@model IEnumerable<Online_Shopping_Management.Models.Category>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
Select from @Model.Count() categories:
</p>
<ul>
@foreach (var categories in Model)
{
<li>@Html.ActionLink(categories.Name, "Browse", new { categories = categories.Name })</li>
}
</ul>