In my MVC application, I am using following code for a file.
MODEL
public HttpPostedFileBase File { get; set; }
VIEW
@Html.TextBoxFor(m => m.File, new { type = "file" })
Everything working fine .. for submitting value But I am trying to load file from controller model which is not working
CONTROLLER
public ActionResult ManagePhotos(ManagePhoto model)
{
if(ModelState.IsValid)
{
//upload file
}
else
{
return View(model); //contains type HttpPostedFileBase File { get; set; }
}
}
how can i load file input again if my validation fails as after returning, my file control is not mapped to model to file and it’s empty…
If you know that the file is valid, and want to keep it temporarily, you can keep it in session (but be aware of memory usage).
If you know the file is valid, and want to keep it permanently, save it and just keep the path in memory.
If you know that the file is invalid, you wont want to nor often be able to keep it.
Often this is treated in the same way during validation failure as a password – it needs to be provided again and as such is requested for only when everything else is good.