Hi, I am developing an application in MVC and I want to save an image in a table called Inventory in my database but it always tells me that it can not return null
Here is my code
My model
public partial class Inventario
{
public int IdProducto { get; set; }
[DisplayName("Producto")]
public string Nom_Producto { get; set; }
[DisplayName("Precio por unidad")]
public Nullable<decimal> Precio_Uni { get; set; }
public Nullable<int> Cantidad { get; set; }
[DisplayName("Categoria")]
public Nullable<int> IdCategoria { get; set; }
[DisplayName("Cargar Imagen")]
public string ImagePath { get; set; }
public HttpPostedFileBase ImageFile { get; set; }
}
My sight
@using (Html.BeginForm("Create", "AccionesInventarios", FormMethod.Post, new {enctype = "multipart/form-data" }))
<input type="file" name="ImageFile" required>
My controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IdProducto,Nom_Producto,Precio_Uni,Cantidad,IdCategoria,ImagePath")] Inventario inventario)
{
string fileName = Path.GetFileNameWithoutExtension(inventario.ImageFile.FileName);
string extension = Path.GetExtension(inventario.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
inventario.ImagePath = "~/Image/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
inventario.ImageFile.SaveAs(fileName);
if (ModelState.IsValid)
{
db.Inventarios.Add(inventario);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(inventario);
}