Am working with the built-in template of visual studio 2013 mvc 5. am trying to display images i saved in the database to the index view of the home page. (code first approach)
//my model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace HaveYouSeenMeApp.Models
{
public class PetPhotos
{
public int PetPhotosId { get; set; }
[StringLength(255)]
public string PetPhotoName { get; set; }
[StringLength(100)]
public string ContentType { get; set; }
public byte[] Content { get; set; }
public PetPhotosType PetPhotosType { get; set; }
public int PetID { get; set; }
public virtual Pet Pet { get; set; }
public virtual ApplicationUser User { get; set; }
}
}
//my viewmodel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HaveYouSeenMeApp.Models.ViewModels
{
public class ImageViewModel
{
public string PetPhotoName { get; set; }
public byte[] Content { get; set; }
}
}
// my controller
using HaveYouSeenMeApp.Models;
using HaveYouSeenMeApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HaveYouSeenMeApp.Controllers
{
public class HomeController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
ApplicationDbContext db = new ApplicationDbContext();
List ImageViews = new List();
var ImageList = (from cust in db.PetPhotoSS select new { cust.PetPhotoName, cust.Content }).ToList();
foreach(var item in ImageList)
{
ImageViewModel objcvm = new ImageViewModel();
objcvm.PetPhotoName = item.PetPhotoName;
objcvm.Content = item.Content;
ImageViews.Add(objcvm);
}
return View(ImageViews);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
//my view the yellow highlighted sections is where i want to display my images
@model IEnumerable
@{
ViewBag.Title = "Home Page";
}
ASP.NET
ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.
Getting started
| @Html.DisplayNameFor(model => model.PetPhotoName) | @Html.DisplayNameFor(model => model.Content) |
|---|---|
@Html.DisplayFor(modelItem => item.PetPhotoName) | @Html.DisplayFor(modelItem => item.Content) |
Get more libraries
NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
Web Hosting
You can easily find a web hosting company that offers the right mix of features and price for your applications.
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Catcher WongPosted Feb 9, 2017, 10:52 PM