Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an image from a path in asp.net MVC 4 and Razor view?

I have the following model:

public class Player {       public String ImagePath     {         get         {             return "~/Content/img/sql_error.JPG";         }      } 

And, this is my .cshtml file:

@model SoulMasters.Models.Game.Player  @{ ViewBag.Title = "Game"; Layout = "~/Views/Shared/_GameLayout.cshtml"; var imagePath = @Model.ImagePath; }  <fieldset> <legend>Player</legend>     <div class="display-field">     @Html.DisplayFor(model => model.ImagePath) </div> @imagePath <div style="padding:10px;">      <img src=@imagePath alt="Sample Image" width="300px" />      <img  alt="asd" >         model.ImagePath;     </img> </div> </fieldset> 

The result is simple text:

~/Content/img/sql_error.JPG ~/Content/img/sql_error.JPG Sample Image asd model.ImagePath; 

Also, I have tried the following line, and it works:

<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" /> 

How to display that image from path? Could the image path be different based on settings? Any other ideas?

I don't really get it now on how razor syntax works.

like image 937
Sas Gabriel Avatar asked Apr 13 '13 10:04

Sas Gabriel


People also ask

How do I display an image in ASPX?

To upload the image, click on Choose File and then browse to the image which you want to upload. Once the image is selected then the name of the image will be displayed next to the Choose File button as shown in the following screenshot. As you can see the that images.

How do you use a model in razor view?

Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, and press the Add button. This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.


1 Answers

In your View try like this

  <img src= "@Url.Content(Model.ImagePath)" alt="Image" /> 
like image 111
Adithya Avatar answered Oct 01 '22 03:10

Adithya