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 6979999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:57:27+00:00 2026-05-27T17:57:27+00:00

I believe that I couldn’t find a proper title to explain my problem but

  • 0

I believe that I couldn’t find a proper title to explain my problem but I think this is the best possible short explanation.

Please let me explain the details.

I want to show a list of my pictures on a page and using a @foreach loop on MVC 3.

Partial View of this list as below:

@model IEnumerable<Picture>
@foreach (var item in Model)
{
    <a href="@item.PicId">
        <img height="35px" style="padding-top:3px" src="ImageHandler.ashx?id=@item.PicId" id="pictureMy" />
    </a>
}

As you may understand I am sending a list to this partialview and it is placing the pictures on a single column.

It is working without any problem but I want to show 3 pictures for each row but couldn’t manage.

Any guidance will be very appreciated.

Thanks in advance for your helps.

  • 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-05-27T17:57:28+00:00Added an answer on May 27, 2026 at 5:57 pm

    You could group them by 3:

    @model IEnumerable<Picture>
    @foreach (var item in Model.Select((value, index) => new { value, index }).GroupBy(x => x.index / 3))
    {
        <div>
        @foreach (var picture in item)
        {
            <a href="@picture.value.PicId">
                <img height="35px" style="padding-top:3px" src="ImageHandler.ashx?id=@picture.value.PicId" id="pictureMy" />
            </a>
        }
        </div>
    }
    

    But honestly this grouping is not something that should be done in the view. You should define a view model and then have your controller action perform the grouping and return the view model.

    So let’s start by defining our view models:

    public class PictureViewModel
    {
        public int PicId { get; set; }
    }
    
    public class GroupedPicturesViewModel
    {
        public IEnumerable<PictureViewModel> Pictures { get; set; }
    }
    

    then the controller action:

    public ActionResult Index()
    {
        // fetch the pictures from the DAL or something
        IEnumerable<Picture> pictures = ... 
    
        // Now build the view model
        var model = pictures
            .Select((value, index) => new { value, index })
            .GroupBy(x => x.index / 3)
            .Select(x => new GroupedPicturesViewModel
            {
                Pictures = x.Select(p => new PictureViewModel
                {
                    PicId = p.value.PicId
                })
            }
        );
        return View(model);
    }
    

    then the corresponding view:

    @model IEnumerable<GroupedPicturesViewModel>
    @Html.DisplayForModel()
    

    then the corresponding display template for the GroupedPicturesViewModel type (~/Views/Shared/DisplayTemplates/GroupedPicturesViewModel.cshtml):

    @model GroupedPicturesViewModel
    <div>
        @Html.DisplayFor(x => x.Pictures)
    </div>
    

    and finally the display template for the PictureViewModel type (~/Views/Shared/DisplayTemplates/PictureViewModel.cshtml):

    @model PictureViewModel
    <a href="@Model.PicId">
        <img class="image" src="@Url.Content("~/ImageHandler.ashx?id=" + Model.PicId)" alt="" />
    </a>
    

    One final thing that’s bugging me is this anchor. Looks ugly. Don’t you think? Looks like spaghetti code.

    Let’s improve it by writing a custom, reusable HTML helper which will render those pictures:

    public static class HtmlExtensions
    {
        public static IHtmlString Picture(this HtmlHelper<PictureViewModel> htmlHelper)
        {
            var anchor = new TagBuilder("a");
            var picture = htmlHelper.ViewData.Model;
            var id = picture.PicId.ToString();
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
    
            // You probably need another property on your view model here as this 
            // id is suspicious about href but since that's what you had in your 
            // original code I don't know what is your intent.
            anchor.Attributes["href"] = id;
            var image = new TagBuilder("img");
            image.Attributes["alt"] = "";
            image.Attributes["src"] = urlHelper.Content(
                "~/ImageHandler.ashx?id=" + urlHelper.Encode(id)
            );
            image.AddCssClass("image");
            anchor.InnerHtml = image.ToString();
            return new HtmlString(anchor.ToString());
        }
    }
    

    and then in the display template we will simply have:

    @model PictureViewModel
    @Html.Picture()
    

    And that’s pretty much it. No need to write loops. Everything works by convention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really can't believe I couldn't find a clear answer to this... How do
I believe that Agile is nothing but another implementation of Spiral Model. I am
I don't believe that I am using any 3.0 specific APIs, but somehow whenever
I'm looking at hashing algorithms, but couldn't find an answer. Bcrypt uses Blowfish Blowfish
I have reminder with a known ID, but couldn't find a solution to delete
I couldn't find another answer when I wanted this, so I thought I would
(Apologies if this has been asked before - I can't believe it hasn't, but
I have seen couple of similar problems with solutions, but I couldn't find one
I was lead to believe that MVC apps were BIN-deployable, so could be deployed
I believe that quantifying the productivity increase (extra working hours) is the most effective

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.