0

I implemented a PartialView Load but the (img tag) is not displayed an image. There was no error and the alt="IMAGE" was displayed instead.

Home/Index.cshtml

<div id="partialviews">
</div>

<script type="text/jscript">
$(document).ready(function () {
    $("#partialviews").load('/home/GetImage');
});
</script>

Home/HomeController.cs

public PartialViewResult GetImage(string category)
{
    return PartialView("_ShowImage");
}

_ShowImage.cshtml

<div>
    @Html.Label("Image:")
    @{ var imagePath = @"~/Images/cctv.jpg"; }
    <div>
        <img scr="@Url.Content(@imagePath)" alt="IMAGE" />
    </div>
</div>

Project directory structure

mqb
  • 1
  • 2
  • can you show your folder structure of your project containing the partial view and image? – Jeric Cruz Aug 31 '17 at 06:10
  • May be this answer from an another [post](https://stackoverflow.com/a/1268755/5918240) can help you. – Tolotra RAHARISON Aug 31 '17 at 07:27
  • 1
    `App_Data` is a special folder (used for data such as database files) and will not render out any contents on the web. Place you images in the `Images` folder –  Aug 31 '17 at 07:28
  • I moved the jpg to Images folder but still the image is not displayed. – mqb Aug 31 '17 at 15:37

1 Answers1

0

Just do

<div>
    <img scr="~/Images/cctv.jpg" alt="IMAGE" />
</div>

If this does not work, the image path is not correct. And check, if the folder Images is included to your project.

iquellis
  • 979
  • 1
  • 8
  • 26
  • I took out the ~ in front of the imagePath and it works now! It is a new Visual Studio 2017 project with .NET 4.5.2; I used ~ before and it has no problem til now. Any idea why? – mqb Aug 31 '17 at 17:19
  • Did the folder infrastructure change? – iquellis Aug 31 '17 at 18:41
  • No, folder infrastructure is the same as posted earlier. – mqb Aug 31 '17 at 20:52