Good morning
I'm starting on asp.net and I'm having a problem that I can not find the solution for, because I do not understand why it works exactly.
What happens is that when you send a form to the controller, the actionResult receives an object.
[HttpPost]// con esta linea se presenta el problema.
public ActionResult SinRedimensionar(TrabajaImagen imagen)
{
if (imagen.Fichero != null && imagen.Fichero.ContentLength > 0)
{
try
{
Image.FromStream(imagen.Fichero.InputStream);
string nombreFichero = Path.GetFileNameWithoutExtension(imagen.Fichero.FileName) + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(imagen.Fichero.FileName);
imagen.Fichero.SaveAs(Path.Combine(Server.MapPath("~/Imagenes"), nombreFichero));
imagen.NombreFichero = nombreFichero;
}
catch
{
ModelState.AddModelError(string.Empty, "La imagen seleccionada no es una imagen válida");
}
}
else
{
ModelState.AddModelError(string.Empty, "No se ha seleccionado ninguna imagen");
}
return View(imagen);
}
When I add the [HttpPost] I get the error of not found 404. but if I remove it, no. And I can not understand why this is happening, how could this be solved and how is the correct way to make it work?
It should be clear that from the view if I am sent information by the post method.
@using (Html.BeginForm("SinRedimensionar", "Fichero", null, FormMethod.Post, new { enctype = "multipart/form-data", role = "form" }))
{
@Html.HiddenFor(m => m.NombreFichero)
<div class="col-md-12">
<input id="btnSubmit" type="submit" value="Subir" />
</div>
@Html.ValidationSummary(false)
} I appreciate your help.