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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:58:53+00:00 2026-05-22T02:58:53+00:00

I’m rather new to MVC and have recently started a Asp.Net MVC3 project, using

  • 0

I’m rather new to MVC and have recently started a Asp.Net MVC3 project, using the Razor view engine.

At the moment I’m having trouble with outputting an image name into an html img tag with Razor.

Say I have a Car class in my model, with a string property called ModelName. I also have a folder with images of the car model, they are named after the car model so that I can by convention show the image of a specific model, by just using the name.

From my controller I pass down a collection of Cars to my view and do the following to show a list of car model images:

@foreach (var item in Model) {
<img src='@item.ModelName.png' />
}

However if I try to run this, I get the error:

Compiler Error Message: CS1061:
‘string’ does not contain a definition
for ‘png’ and no extension method
‘png’ accepting a first argument of
type ‘string’ could be found (are you
missing a using directive or an
assembly reference?)

I also tried

    @foreach (var item in Model) {
     <img src='@{item.ModelName}.png' />
    }

But that results in:

Compiler Error Message: CS1528: Expected ; or = (cannot specify constructor arguments in >declaration)
Source Error:

Line 84: #line default
Line 85: #line hidden Line
86: WriteLiteral(“.png\’
“>\r\n”); Line 88:

I can get around it by doing:

@foreach (var item in Model) {
     string imageName = string.Format("{0}.png", item.ModelName);
     <img src='@imageName' />
    }

But that feels quite clunky, surely there must be a better way??

  • 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-22T02:58:54+00:00Added an answer on May 22, 2026 at 2:58 am

    You can also do:

    @foreach (var item in Model) 
    {
        <img src="@(item.ModelName).png" />
    }
    

    Or make a Helper:

    public static MVCHtmlString Image<T>(this HtmlHelper helper, string name)
    {
        TagBuilder img=new TagBuilder("img");
        img.Attributes.Add("src", name+".png");
        return new MvcHtmlString(img.ToString());
    }
    

    and use it like:

    @foreach (var item in Model) 
    {
        @Html.Image(item.ModelName)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.