0

Why I get null value in input file when return model to view?

this my model:

public class BookViewModel
{
    public string Author{ get; set; }
    public HttpPostedFileBase Book { get; set; }
}

this my Controller:

[HttpPost]
public ActionResult UploadBook(BookViewModel model)
{
   // DO Stuff
   return View(model);
}

this my View:

@using (Html.BeginForm("UploadBook", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(m => m.Author)
    <br /><br />

    <input type="file" name="Book" /><br />
    <input type="submit" value="submit me"/>
}

Now, when I submit, the model the file upload but when return to view it return null why. please help me.

Thanks.

Ahmed Saif
  • 23
  • 1
  • 8
  • Try `enctype = "multipart/form-data"` instead of `encType = "multipart/form-data"` – Jasen Jul 06 '21 at 18:39
  • @Jasen still not work. – Ahmed Saif Jul 06 '21 at 19:21
  • You can't return file content like that. See answers here https://stackoverflow.com/q/5826649/2030565 – Jasen Jul 06 '21 at 20:16
  • @Jasen Is there any trick to do that? – Ahmed Saif Jul 07 '21 at 04:27
  • I do not want users to select the file every time if they already selected it. – Ahmed Saif Jul 07 '21 at 04:34
  • When model data come from view to post action result it is ok no null value but when send it back to view the name of author return but the file not return to input file shown choose file but the user already been selected first time why is delete i debug in post action it is not null and in view not null why not replace it in input file. – Ahmed Saif Jul 07 '21 at 20:42
  • You can't do that for browser security reasons. However, you can upload the file with JavaScript and FormData so you never navigate away from the page (the form remains intact). https://stackoverflow.com/q/2428296/2030565 – Jasen Jul 07 '21 at 21:05
  • How can get over browser security. – Ahmed Saif Jul 08 '21 at 10:39
  • Is there any way to re-show file name in input file and save file temporary in the server? – Ahmed Saif Jul 08 '21 at 10:48

0 Answers0