Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:38:53+00:00 2026-06-11T21:38:53+00:00

@foreach (var item in Model) { <img src=’ShowShowcaseImage/@Html.Encode(item.ProductID)’ id=’@item.ProductID’ /> <b>@Html.DisplayFor(m => item.ProductName)</b> <a

  • 0
  @foreach (var item in Model)
  { 
        <img src='ShowShowcaseImage/@Html.Encode(item.ProductID)' id='@item.ProductID'  />
        <b>@Html.DisplayFor(m => item.ProductName)</b>
        <a href="#"  class="enlargeImg" id="@item.ProductID">Enlarge</a>
  }

<div id="EnlargeContent" class="content">
    <span class="button bClose"><span>X</span></span>

    <div style="margin: 10px;" id="imageContent">
    </div>

    <p align="center"></p>
</div>

//Popup javascript

$('.enlargeImg').bind('click', function (e) {
            $.post('/Home/EnlargeShowcaseImage/' + $(this).attr('id'), null, function (data) {
         document.getElementById("imageContent").innerHTML +=  data;
            });

            $('#EnlargeContent').bPopup();
 });
    });

//
C# method

  public ActionResult EnlargeShowcaseImage(string id)
            {

                var imageData = //linq query for retrive bytes from database;
                StringBuilder builder = new StringBuilder();
                if (imageData != null)
                    builder.Append("<img src='" + imageData.ImageBytes + "' />");
                return Json(builder);

            }

I want to show pop up of enlarged image on click of enlarge link. Image is stored in bytes in database. Two images are stored in database for each product – one is thumbnail and the other is enlarged. I am showing thumbnail image and I want to show enlarged image on click of enlarge link. I can’t retrieve it from database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:38:54+00:00Added an answer on June 11, 2026 at 9:38 pm

    I can’t retrieve it from database

    So your question is about retrieving an image from a database, right? It has strictly nothing to do with ASP.NET MVC?

    Unfortunately you haven’t told us whether you are using some ORM framework to access to your database or using plain ADO.NET. Let’s assume that you are using plain ADO.NET:

    public byte[] GetImage(string id)
    {
        using (var conn = new SqlConnection("YOUR CONNECTION STRING COMES HERE"))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            // TODO: replace the imageData and id columns and tableName with your actual
            // database table names
            cmd.CommandText = "SELECT imageData FROM tableName WHERE id = @id";
            cmd.Parameters.AddWithValue("@id", id);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                {
                    // there was no corresponding record found in the database
                    return null;
                }
    
                const int CHUNK_SIZE = 2 * 1024;
                byte[] buffer = new byte[CHUNK_SIZE];
                long bytesRead;
                long fieldOffset = 0;
                using (var stream = new MemoryStream())
                {
                    while ((bytesRead = reader.GetBytes(reader.GetOrdinal("imageData"), fieldOffset, buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, (int)bytesRead);
                        fieldOffset += bytesRead;
                    }
                    return stream.ToArray();
                }            
            }
        }
    }
    

    and if you are using some ORM it could be as simple as:

    public byte[] GetImage(string id)
    {
        using (var db = new SomeDataContext())
        {
            return db.Images.FirstOrDefault(x => x.Id == id).ImageData;
        }
    }
    

    and then inside your controller action:

    public ActionResult EnlargeShowcaseImage(string id)
    {
        var imageData = GetImage(id);
        if (imageData != null)
        {
            // TODO: adjust the MIME Type of the images
            return File(imageData, "image/png");
        }
    
        return new HttpNotFoundResult();
    }
    

    and it is inside your view that you should create an <img> tag pointing to this controller action upon button click:

    $('.enlargeImg').bind('click', function (e) {
        $('#imageContent').html(
            $('<img/>', {
                src: '/Home/EnlargeShowcaseImage/' + $(this).attr('id')
            })
        );
        $('#EnlargeContent').bPopup();
    });
    

    But hardcoding the url to your controller action in javascript like this is very bad practice because when you deploy your application it might break. It might also break if you decide to change the pattern of your routes. You should never hardcode urls like this. I would recommend you generating this url on the server.

    For example I see that you have subscribed to some .enlargeImage element. Let’s suppose that this is an anchor. Here’s how to properly generate it:

    @Html.ActionLink("Enlarge", "EnlargeShowcaseImage", "Home", new { id = item.Id }, new { @class = "enlargeImage" })
    

    and then adapt the click handler:

    $('.enlargeImg').bind('click', function (e) {
        // Cancel the default action of the anchor
        e.preventDefault();
    
        $('#imageContent').html(
            $('<img/>', {
                src: this.href
            })
        );
        $('#EnlargeContent').bPopup();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why in the following Razor markup @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem
I have a loop <ul id=news-list class=thumbobs-list> @foreach (var item in Model.News) { @Html.Partial(RenderNews/
Hi I have some code like: foreach (var item in Model) { <div> @Html.DisplayFor(modelItem
I have a table like: @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem
Code: <% foreach (var item in Model) { %> <td> <%= Html.Encode(item.BirthDate) %> </td>
<div class=newstitle> <ul class=newstitle> @foreach (var item in Model.Content) { if (item.Value != null)
<div id=detailed> @foreach (var item in Model.Result.Items) { <div id=movie_@(movie.UserMovieID) class=movie border-gray> <!-- Some
I have this html @foreach (var item in Model.Options) { <input type=checkbox checked=checked name=selectedObjects
This is My View: @foreach(var item in Model) { <tr id=TR@(item.Id)> @{Html.RenderPartial(_PhoneRow, item);} </tr>
I have the following code @foreach (var item in Model.Defaults) { <tr class=CertainCategory> <td>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.