mvc musicstore sample application loads cart images using code below. In my application product images are store in database. How to modify this code so that images are loaded from database ? <ul id="product-list"> <% { %> foreach (var product in Model)<li> <a href="<%= Url.Action("Details", "Store", new { id = product.Contents }) %>"> <img height="100" width="100" alt="<%= product.Name %>" src="Content/ProductImages/<%= product.Contents %>.jpg" /> <span><%= product.Name %></span> </a> </li> <% } %> </ul>
Create the following action in the Store controller: public FileContentResult GetImage( int productId ) { var product = this._productsRepository.Products.First( x => x.ProductID == productId ); return File( product.ImageData, product.ImageMimeType ); } where ImageData is a byte array (byte[]) contained in a varbinary field and ImageMimeType is the mimetype for each image (image/png, etc) in your view use it like this: <img src="<%: Url.Action("GetImage", "Store", new { product.ProductID }) %>" />