Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could group them by 3:</p> <pre><code>@model IEnumerable&lt;Picture&gt; @foreach (var item in Model.Select((value, index) =&gt; new { value, index }).GroupBy(x =&gt; x.index / 3)) { &lt;div&gt; @foreach (var picture in item) { &lt;a href="@picture.value.PicId"&gt; &lt;img height="35px" style="padding-top:3px" src="ImageHandler.ashx?id=@picture.value.PicId" id="pictureMy" /&gt; &lt;/a&gt; } &lt;/div&gt; } </code></pre> <p>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. </p> <p>So let's start by defining our view models:</p> <pre><code>public class PictureViewModel { public int PicId { get; set; } } public class GroupedPicturesViewModel { public IEnumerable&lt;PictureViewModel&gt; Pictures { get; set; } } </code></pre> <p>then the controller action:</p> <pre><code>public ActionResult Index() { // fetch the pictures from the DAL or something IEnumerable&lt;Picture&gt; pictures = ... // Now build the view model var model = pictures .Select((value, index) =&gt; new { value, index }) .GroupBy(x =&gt; x.index / 3) .Select(x =&gt; new GroupedPicturesViewModel { Pictures = x.Select(p =&gt; new PictureViewModel { PicId = p.value.PicId }) } ); return View(model); } </code></pre> <p>then the corresponding view:</p> <pre><code>@model IEnumerable&lt;GroupedPicturesViewModel&gt; @Html.DisplayForModel() </code></pre> <p>then the corresponding display template for the <code>GroupedPicturesViewModel</code> type (<code>~/Views/Shared/DisplayTemplates/GroupedPicturesViewModel.cshtml</code>):</p> <pre><code>@model GroupedPicturesViewModel &lt;div&gt; @Html.DisplayFor(x =&gt; x.Pictures) &lt;/div&gt; </code></pre> <p>and finally the display template for the <code>PictureViewModel</code> type (<code>~/Views/Shared/DisplayTemplates/PictureViewModel.cshtml</code>):</p> <pre><code>@model PictureViewModel &lt;a href="@Model.PicId"&gt; &lt;img class="image" src="@Url.Content("~/ImageHandler.ashx?id=" + Model.PicId)" alt="" /&gt; &lt;/a&gt; </code></pre> <p>One final thing that's bugging me is this anchor. Looks ugly. Don't you think? Looks like spaghetti code. </p> <p>Let's improve it by writing a custom, reusable HTML helper which will render those pictures:</p> <pre><code>public static class HtmlExtensions { public static IHtmlString Picture(this HtmlHelper&lt;PictureViewModel&gt; 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()); } } </code></pre> <p>and then in the display template we will simply have:</p> <pre><code>@model PictureViewModel @Html.Picture() </code></pre> <p>And that's pretty much it. No need to write loops. Everything works by convention.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload