Hello, I'd like some help.
I have the following controller to register an image and some fields, with the property of the image being string
:
[HttpPost]
[ValidateInput(false)]
[ValidateAntiForgeryToken]
public ActionResult Cadastrar(NoticiaViewModel vm, HttpPostedFileBase file)
{
try
{
vm.ComboCategoriaId = new CategoriaRepositorio().BuscarTodos().Select(x => new SelectListItem { Text = x.NomeCategoria, Value = Convert.ToString(x.CategoriaId) });
if (!ModelState.IsValid)
{
return View(vm);
}
var mapper = Mapper.Map<NoticiaViewModel, Noticia>(vm);
_repositorio.Salvar(mapper);
if (file != null)
{
String[] strName = file.FileName.Split('.');
String strExt = strName[strName.Count() - 1];
string pathSave = String.Format("{0}{1}.{2}", Server.MapPath("~/Imagem/noticias/"), mapper.NoticiaId, strExt);
String pathBase = String.Format("/Imagem/noticias/{0}.{1}", mapper.NoticiaId, strExt);
file.SaveAs(pathSave);
mapper.Imagem = pathSave;
_repositorio.Atualizar(mapper);
}
TempData["mensagem"] = "Noticia cadastrada com sucesso";
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return View();
}
}
So long, I record the path where my image is. The problem is to show it:
public ActionResult Visualizar(int id)
{
try
{
Noticia noticia = _repositorio.BuscarPorId(id);
if (noticia == null)
{
return HttpNotFound();
}
var mapper = Mapper.Map<Noticia, NoticiaViewModel>(noticia);
return View(mapper);
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return View();
}
}
I'm using this to bring the data from a particular database:
The View
looks like this:
<div class="panel-body">
<div class="col-md-12">
<div class="form-group">
<small class="text-right">@Model.DataCadastro</small>
</div>
<div class="form-group">
<h3 class="text-left">Categoria: <b>@Model.Categoria.NomeCategoria</b></h3>
</div>
<div class="form-group">
<h2 class="text-center">Título: @Model.Titulo</h2>
</div>
<div class="form-group">
@MvcHtmlString.Create(Model.Descricao)
</div>
<div class="">
<img src="@Model.Imagem" />
</div>
</div>
</div>
But the image does not appear.
Can anyone give me a hand?