Problem displaying image

Asked

Viewed 127 times

0

As I display an image recorded in the database, I believe that it is not actually recorded, because I can only see the name/format of the image. In this case, how do I display this image?

I looked for some ways to do using custom HTML helpers and so on, but I think I’m forgetting something.

Follow my image upload code:

[HttpPost]
public ActionResult Adicionar(usuario usuario, HttpPostedFileBase foto)
{
    if (ModelState.IsValid)
    {
        db.usuario.Add(usuario);
        db.SaveChanges();
        var fotoperfil = Path.GetFileName(foto.FileName);
        var caminho = Path.Combine(Server.MapPath("~/App_Data/Foto"), fotoperfil);
        foto.SaveAs(caminho);
        return RedirectToAction("Index");
    }
    return View(usuario);               
}
  • It is not really being. In what part the Model User receives the file path?

  • Well, I confess that I did not make any changes to the model. In fact, I didn’t see anyone talking about it. I only made the changes to the view and controller itself.

1 answer

1


You need to save the change in Model, as below:

[HttpPost]
public ActionResult Adicionar(usuario usuario, HttpPostedFileBase foto)
{
    if (ModelState.IsValid)
    {
        db.usuario.Add(usuario);
        db.SaveChanges();
        var fotoperfil = Path.GetFileName(foto.FileName);
        var caminho = Path.Combine(Server.MapPath("~/App_Data/Foto"), fotoperfil);
        foto.SaveAs(caminho);

        usuario.ArquivoDaFoto = caminho;
        return RedirectToAction("Index");
    }
    return View(usuario);               
}

ArquivoDaFoto must be String.

  • Got it. But that’s enough to display the photo in a form, for example?

  • It is yes. You just have to put the content of ArquivoDaFoto within a tag <img>.

  • So simple and I breaking head here. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.