Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the images in the view are just references to image resources like that:</p> <pre><code>images/AmericanGoldfinch_male.jpg </code></pre> <p>then you can loop through your database records to emit the markup for the images. For example, let's assume on your model you have populated an <code>IEnumerable&lt;SomeCustomObject&gt;</code> called <code>Images</code> with the URLs and titles of the image files. In that case your view would contain something like this:</p> <pre><code>&lt;div&gt; Birds&lt;br /&gt;&lt;br /&gt; @foreach (var image in Model.Images) { &lt;a href="@image.Url" rel="lightbox[roadtrip2]" title="@image.Title"&gt; &lt;img src="@image.Url" width="60" height="60" alt="" /&gt; &lt;/a&gt; } &lt;/div&gt; </code></pre> <p>This would generate the same markup as you have manually created, but dynamically for all of the images in the model.</p> <p>The next question then becomes... Are the images referenced by URL from the database or are they actually <em>stored</em> in the database? If the latter, then what you'd probably do is create a separate action method to act as the "image" itself, which dynamically returns the image from the database. First you'd have to change the above code to use that action. Something like this, assuming an image ID instead of a URL on the model:</p> <pre><code>&lt;div&gt; Birds&lt;br /&gt;&lt;br /&gt; @foreach (var image in Model.Images) { &lt;a href="@Url.Action("ActionName", "ControllerName", new { id = image.ID })" rel="lightbox[roadtrip2]" title="@image.Title"&gt; &lt;img src="@Url.Action("ActionName", "ControllerName", new { id = image.ID })" width="60" height="60" alt="" /&gt; &lt;/a&gt; } &lt;/div&gt; </code></pre> <p>The difference now is that instead of using a static URL for the image, you dynamically create one based on the action name, controller name, and ID of the image as a route value. Next you'd need to create the action method. If the image is just a <code>byte[]</code> from your data access layer, then you can just have that action return a <code>File</code> result. Something like this:</p> <pre><code>public ActionResult ActionName(int id) { var file = Files.Get(id); // this would be replaced with however you get your file from the database return File(file.Data, file.ContentType, file.Name); } </code></pre> <p>This is assuming, of course, that you also store a name and content type with the files in the database. If you're storing the raw file data in the database itself then it's a very good idea to at least store its content type as well.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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