4

i know that there is a lot of questions like this, but i read them all and null of them solved my problem. Here below im going to show the code that i have in my view and my controller so that you can have an idea of my code.

View :
<td><% using (Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) { %>
<%: Html.ValidationSummary(true) %>
                <div id="section1"> 
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Name) %>
    </div>
<label for="file">Filename:</label>
        <input type="file" name="myfile" id="File5" value="Choose File Banner1" />
     <p>&nbsp;</p>

And here is my controller:
[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(HomeModel model, string submitButton, HttpPostedFileBase myfile)
    {

I cant figure out what mite be the problem.. can someone help me please

user2191421
  • 69
  • 3
  • 8

6 Answers6

11

I also had the same problem and I ended up here while looking for an answer. I had forgotten the enctype parameter in the form definition

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
// form
}
Memet Olsen
  • 4,578
  • 5
  • 40
  • 50
0

I had the same problem before, I had to remove the value from the file input to make it work :

<input type="file" name="myfile" id="File5" />
Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69
0

There are a number of causes for this issue:

  • You didn't specify the enctype attribute on the <form> tag
  • You didn't give a name attribute to your <input type="file"> tag
  • You nested a <form> tag in another <form>

If none of these are your issue, this is what worked for me: add an HttpPostedFileBase property to your view model (or create a view model with that property).

View Model

public class MyViewModel
{
    public HttpPostedFileBase MyFile { get; set; }
}

Controller

public ActionResult Index()
{
    return View(new MyViewModel());
}

[HttpPost]
public ActionResult Index(MyViewModel viewModel)
{
    HttpPostedFileBase file = viewModel.MyFile;

    if (file != null && file.ContentLength > 0)
    {
        // Your code here
    }
}
Thomas Higginbotham
  • 1,662
  • 20
  • 25
0

Try using html attribute "@data_ajax = "false" " in the view, near the enctype

(Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data", @data_ajax = "false" }))
Mangal
  • 87
  • 2
  • 11
0

Could it be that you have a value="" set in the input element?

Warren
  • 384
  • 4
  • 17
0

I copied a UI into my working MVC3 application and found upload part starts giving same error. I found out there was an extra form (making it a nested form) in Page Layout. I removed it and everything worked fine for me.