Solved

How to dynamically pass in <item.Id> and <item.ImageUrl> in an Image Tag

Posted on 2013-05-16
2
273 Views
Last Modified: 2013-05-20
Hi,

I'm finding it difficult to pass in the id and the imageUrl values from a database into an image tag, as shown below

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<RetailUploader.Website.Models.ViewModels.Images>>" %>
<p class="CreateButtonStyle">
    <%= Html.ActionLink("Create New", "Create", null, new { @class = "createImage", title = "Create Image" })%>
</p>
<table>
    <tr>
        <th>
            card Image
        </th>
    </tr>
<% foreach (var item in Model)
   { %>
    <tr>
        <td>
            <img src="~/Uploads/" + <%: item.Id %> + "/" + <%: item.ImageUrl %> + "") alt="" />
        </td>
    </tr>
   <% } %>
</table>

When the page renders, nothing displays apart from the table.

Is this the best way to render an image from a database?

Thanks,
Nick
0
Comment
Question by:nickward26
2 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39171422
View the source of your rendered html

You should end up with

<a href="somepage.html"><img id="123" src="/images/abc.jpg"></a>
0
 

Author Comment

by:nickward26
ID: 39179525
Hi,

Thanks for your help, I have been able to successfully get the ImageUrl to render on the page, using the below code:

Controller;
        public ActionResult WebBackground(int id = 3)
        {
            return PartialView("WebBackground", db.ImagesCollection.Find(id));
        }

User Control (Partial)

WebBackground.cshtml

@model MvcMusicStore.Models.ViewModels.Images
@Url.Content("~/Content/media/" + Model.Id + "/" + Model.Image + "")

View
    <style type="text/css">
        .header { background: url(@Html.Action("WebBackground")) repeat 0 0;}
    </style>

This seems to work, there maybe better ways to tackle this problem, but this solution allows me to easily render a ImageUrl from the database and store the image files locally on the web server.

Thanks,

Nick
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now