tags:

views:

30

answers:

3

I have the following form on an .NET MVC View:

<form method="post" enctype="multipart/form-data" action="/Video/UploadDocument"> 
<input type="file" id="document1" name="document1"/> 
<input type="submit"  value="Save"/> 
</form> 

And the controller has the following signature that gets called:

public ActionResult UploadDocument(HttpPostedFileBase file) {
    return View();
}

When I break inside the UploadDocument method, the parameter 'file' is null. I've selected a valid document on my desktop and know it contains text. What am I missing to get this file upload working?

A: 

What happens when you make the name of the argument (file) and the name of the input element equal? I recall that the default model binding logic in ASP.NET MVC works using this convention.

Arne
Still null. Changed 'file' to 'document1' and still had null. :(
Josh
+1  A: 

Try to use

HttpPostedFileBase file = Request.Files["document1"];

There's probably something wrong with bindings ([Bind()] attribute).

Edit: And make that method public ActionResult UploadDocument() {}.

Jakub Lédl
Tried that and it worked....totally don't get why that fixes it. Wierd. Thanks though!
Josh
I only have 1 idea why it began to work: If I remeber, after modifying controller code, the application had to be compiled again for changes to take effect. Did you do that?
Jakub Lédl
+1  A: 

This has been answered but I believe the culprit is your signature. Instead of "file", use "document1".

public ActionResult UploadDocument(HttpPostedFileBase document1) 
{ 
    return View(); 
} 

Please try and let me know your result

Syd