2
Hello, I’m having trouble displaying dynamic images using Razor syntax in an Asp.net MVC 5 project.
I have the image path stored in a column in the database, named Imagery.
Below the code of my view:
@foreach (var item in Model) {
<div class="item branding">
<img src="@Html.DisplayFor(modelItem => item.imagem)" class="img-responsive" alt="" />
<div class="works-overlay">
<div class="wo-inner">
<h4>@Html.DisplayFor(modelItem => item.nome)</h4>
</div>
</div>
</div>
}
But when the view is rendered, the src of the image is correct, the way it is saved in the database: ~/Content/images/gallery/my-image-123.jpg. However the complete image path is incorrect, as shown below:
http://localhost:9474/Albuns/~/Content/images/galeria/minha-imagem-123.jpg
Below follows my controller, with a simple listing method.
public ActionResult Index()
{
return View(db.portifolio.ToList());
}
Can anyone help me in how I do to properly display the images? The idea is to store the image path on the server in the database after uploading it. And when rendering, just take the image path.
If someone has a better opinion of storing/displaying images, please comment.
Apparently your Model is concatenating your current page with the database string (which in this case is the image path). Who is formatting the url? It would be the Controller or the Model (I believe your Model only uses
get; set;
)?– Guilherme Nascimento
Yes, my Model is just using get; set;
– Erico Souza
Add the method you are using to concatenate your question?
– Guilherme Nascimento
Erico, in my project I do as follows: in the attribute
src
tagimage
I put it like this and it works, for example: src="@Model.Photo" and that way I can take the photo in my project and display it.– Érik Thiago
@Guilhermenascimento added the result action method. It’s a simple listing, where I take the database record, and one of them is the image path on the server.
– Erico Souza
But this is the method that is causing the problem?
– Guilherme Nascimento
@Guilhermenascimento basically the problem is that I can not display my images. I usually store the path of it in the bank. I used this in the webforms, and in the src tag I used src='<%# Eval("image")%>'... but in the mvc I did not succeed in thinking the same way.
– Erico Souza
When I say method I mean the controller function, this index function is the one you use to concatenate the path with /albuns/
– Guilherme Nascimento
Do not concatenate, I have stored in the database the image path "~/Content/images/gallery/my-image-123.jpg"
– Erico Souza
I understand @Ericosouza, is that because you are using MVC I believe that we all thought that the images worked with "routes", but after seeing the "update" of your question and your comment, I believe the path displayed on
src
be "static". See my answer.– Guilherme Nascimento