Hello, I recently began to study asp.net mvc and decided not to make a big project, but I ran into a problem. I need to get an array of bytes from the Picture table, which is the element collection of the Users table.

@model IEnumerable<Aut.Models.ApplicationUser> @foreach (var item in Model) { <tr> <td> @Html.Raw("<img style='width:80px; height:60px;' src=\"data:image/jpeg;base64," + Convert.ToBase64String(item.Pictures) + "\" />") </td> </tr> } 

but the maximum that I can do is refer to picks as an element of the database. I used the asp.net identity for authorization and linked the Pictures table to usersid.

 public class ApplicationUser : IdentityUser { public ICollection<Picture> Pictures { get; set; } public ApplicationUser() { Pictures = new List<Picture>(); } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); return userIdentity; } } public class Picture { public int Id { get; set; } public string Size { get; set; } // размер картинки public byte[] Image { get; set; } public int? UserId { get; set; } public ApplicationUser ApplicationUser { get; set; } } 

Well, at the end of my controller, which should take the user ID and the image byte array

  [HttpPost] public ActionResult Create(ApplicationUser au ,Picture pic, HttpPostedFileBase uploadImage) { if (ModelState.IsValid && uploadImage!=null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(uploadImage.InputStream)) { imageData = binaryReader.ReadBytes(uploadImage.ContentLength); } pic.Image = imageData; au.Pictures.Add(pic); db.Users.Add(au); db.SaveChanges(); return RedirectToAction("UserDetails"); } return View(pic); } 

Thanks in advance for your help, I will be very grateful

    1 answer 1

    You need to pass the Picture.Image property to src Picture.Image Try this:

     @model IEnumerable<Aut.Models.ApplicationUser> @foreach (var item in Model) { foreach (var picture in item.Pictures) { <tr> <td> @Html.Raw("<img style='width:80px; height:60px;' src=\"data:image/jpeg;base64," + Convert.ToBase64String(picture.Image) + "\" />") </td> </tr> } } 
    • , thank you very much, for the first time turned on stakoferflow and such luck - Artem Danchenko