crystyxn
crystyxn

Reputation: 1601

Displaying an image in a foreach

So I have login with LinkedIn and I'm trying to add a Facebook one.

I already have the "Signin with LinkedIn" button working, and am trying to add the Facebook one:

<div id="socialLoginList">
                <p>
                    @foreach (AuthenticationDescription p in loginProviders)
                    {
                        var source = "~/Img/" + p.AuthenticationType + ".png";
                        <input type="image" src="@source" border="0" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" style="max-height: 100%; max-width: 100%">
                    }
                </p>
            </div>

If I don't build the path like this, and give it src="~/Img/Facebook.png" or src="~/Img/LinkedIn.png" they work. How can I write the path so the pictures don't appear as broken elements on the page?

Upvotes: 0

Views: 362

Answers (2)

kgzdev
kgzdev

Reputation: 2885

src="@Url.Content(String.Format("~/Img/{0}", source))"

Upvotes: 2

Curiousdev
Curiousdev

Reputation: 5788

If you are using c# 6.0 than you can use Interpolated Strings it'll give you same thing but in much cleaner way

src="@Url.Content($"~/Img/{@source}")"

Upvotes: 1

Related Questions