1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

loading images from database in mvcmusicstore sample

Discussion in 'C#' started by ca4nul, Feb 14, 2011.

  1. #1
    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>



    <% } %>

    ca4nul, Feb 14, 2011 IP
  2. DmitryS

    DmitryS Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    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 }) %>" />
    DmitryS, Feb 16, 2011 IP