title: How to display an image from the database in ASP.NET Core MVC?
body: <p>
  I am quite new with MVC Core and trying to add an image to my SQL Server-database through Entity Framework. I have accomplished the database itself and the view. What I cannot really get working is the controller. Can someone help me get the controller in place. Please!
  
      Here is the model:</p>
  
  <pre><code> public class Product
   {
       [Key]
       public int ProductID { get; set; }
       [Required(ErrorMessage = "Please enter an product name")]
       public string Name { get; set; }
       [Required(ErrorMessage = "Please specify a category")]
       public string Category { get; set; }
       public string SubCategory { get; set; }
       [Required(ErrorMessage = "Please enter a description")]
       public string Description { get; set; }
       [Required(ErrorMessage = "Please enter a positive price")]
       public decimal Price { get; set; }
       public byte[] Image { get; set; }
       public string ImageSourceFileName { get; set; }
       public string ImageContentType { get; set; }
  }
  </code></pre>
  
  <p>Here is the database: </p>
  
  <pre><code> Product ID   int False   
   Category    nvarchar(MAX)   False   
   Description nvarchar(MAX)   False   
   Name    nvarchar(MAX)   False   
   Price   decimal(18,2)   False   
   SubCategory nvarchar(MAX)   True    
   Image   varbinary(MAX)  True    
   ImageContentType    nvarchar(MAX)   True    
   ImageSourceFileName nvarchar(MAX)   True
  </code></pre>
  
  <p>Here is the view: </p>
  
  <pre><code>    &lt;div class="col-md-4"&gt;
      &lt;form asp-action="Create" method="post" enctype="multipart/
       form-data" asp-controller="Products"&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          &lt;div class="form-group"&gt;
                  &lt;label asp-for="Image" class="control-label"&gt;File  
        input&lt;/label&gt;
                  &lt;input asp-for="Image" type="file"
          aria-describedby="fileHelp" class="form-control-file" /&gt;
                  &lt;span asp-validation-for="Image" class="text-danger"&gt;&lt;/span&gt;
                  &lt;small id="fileHelp" class="form-text text-muted"&gt;This is  
       some placeholder block-level help text for the above input. It's a bit
       lighter and easily wraps to a new line.&lt;/small&gt;
              &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="ImageSourceFileName"     
        class= "control-label"&gt;&lt;/label&gt;
              &lt;input asp-for="ImageSourceFileName" class="form-control" /&gt;
              &lt;span asp-validation-for="ImageSourceFileName"   
        class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="ImageContentType" class="control-label"&gt;&lt;/label&gt;
              &lt;input asp-for="ImageContentType" class="form-control" /&gt;
              &lt;span asp-validation-for="ImageContentType" 
              class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is the controller:</p>
  
  <pre><code>    public class ProductsController : Controller
      {
      private readonly ApplicationDbContext _context;
  
      public ProductsController(ApplicationDbContext context)
      {
          _context = context;
      }
  
      // GET: Products
      public async Task&lt;IActionResult&gt; Index()
      {
          return View(await _context.Products.ToListAsync());
      }
  
      // GET: Products/Details/5
      public async Task&lt;IActionResult&gt; Details(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          var product = await _context.Products
              .SingleOrDefaultAsync(m =&gt; m.ProductID == id);
          if (product == null)
          {
              return NotFound();
          }
  
          return View(product);
      }
  
      // GET: Products/Create
      public IActionResult Create()
      {
          return View();
      }
  
              [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create
          ([Bind("ProductID,Name,Category,SubCategory,
           Description,Price,Image,ImageSourceFileName,ImageContentType")]
          Product product)
      {
          if (ModelState.IsValid)
          {
              _context.Add(product);
              await _context.SaveChangesAsync();
              return RedirectToAction(nameof(Index));
          }
          return View(product);
      }
  
      // GET: Products/Edit/5
      public async Task&lt;IActionResult&gt; Edit(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          var product = await _context.Products.SingleOrDefaultAsync(m =&gt;
           m.ProductID == id);
          if (product == null)
          {
              return NotFound();
          }
          return View(product);
      }
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; 
          Edit(int id, 
          [Bind 
      ("ProductID,Name,Category,SubCategory,Description,Price,
       Image,ImageSourceFileName,ImageContentType")]
          Product product)
      {
          if (id != product.ProductID)
          {
              return NotFound();
          }
  
          if (ModelState.IsValid)
          {
              try
              {
                  _context.Update(product);
                  await _context.SaveChangesAsync();
              }
              catch (DbUpdateConcurrencyException)
              {
                  if (!ProductExists(product.ProductID))
                  {
                      return NotFound();
                  }
                  else
                  {
                      throw;
                  }
              }
              return RedirectToAction(nameof(Index));
          }
          return View(product);
      }
  
      // GET: Products/Delete/5
      public async Task&lt;IActionResult&gt; Delete(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          var product = await _context.Products
              .SingleOrDefaultAsync(m =&gt; m.ProductID == id);
          if (product == null)
          {
              return NotFound();
          }
  
          return View(product);
      }
  
      // POST: Products/Delete/5
      [HttpPost, ActionName("Delete")]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; DeleteConfirmed(int id)
      {
          var product = await _context.Products.SingleOrDefaultAsync(m =&gt;  
       m.ProductID == id);
          _context.Products.Remove(product);
          await _context.SaveChangesAsync();
          return RedirectToAction(nameof(Index));
      }
  
      private bool ProductExists(int id)
      {
          return _context.Products.Any(e =&gt; e.ProductID == id);
      }
  }
  </code></pre>
  
  <p>
  I like to store the image in the database. Now all text info goes into the database nice and neat, but no image/bytes...</p>
  
  <p>
  The space is empty in the index-view where the image is supposed to be. </p>