0

I am trying to add a feature to my ASP.NET web application that will allow a user to upload a file that contains a list of courses for a student. I added the upload feature to my view and created a new method in my controller. Every time I select a file and click upload, I get the following error: "An exception of type 'System.NullReferenceException' occurred". I am guessing my file variable is coming is as null? Below is my controller code and my view code:

View:

<div id="uploadCourseList">
        @using (Html.BeginForm("uploadCourseList", "ManageStudentCourses", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.TextBoxFor(x => Model.userId, @Model.userId, new { @class = "addCourseTxtBoxId" })
            <table class="courseListTable">
                <tr>
                    <th colspan="4">
                        Upload Course List
                    </th>
                </tr>
                <tr>
                    <td>
                        Select a file:
                    </td>
                    <td>
                        <input type="file" name="file" id="file" />
                    </td>
                    <td>
                        <input type="submit">
                    </td>
                </tr>
            </table>
        }
    </div>

Controller

public PartialViewResult uploadCourseList(HttpPostedFileBase file, courseListViewModel modelView)
    {
        if (file.ContentLength > 0 && file != null)
        {
            string path = Path.Combine(Server.MapPath("~/Content/courseList"), Path.GetFileName(file.FileName));
            file.SaveAs(path);
        }
     return PartialView("~/Views/ManageStudentCourses/listCourses.cshtml");
    }
Stc5097
  • 291
  • 1
  • 11
  • 25
  • Have you tried to put a break point inside your controller? – ekad Oct 04 '14 at 17:20
  • check this SO question it will guide you how to upload file:http://stackoverflow.com/questions/25125127/asp-net-mvc-4-c-sharp-httppostedfilebase-how-do-i-store-file – Ehsan Sajjad Oct 04 '14 at 17:55
  • Your code seems to be fine, it is working without any problem on my side. I only doubt your `TextBoxFor` code. I commented out that code and Apart from that everything was working fine. – ramiramilu Oct 04 '14 at 19:33

3 Answers3

0

You have two main problems here:

  1. The attribute on your form tag should be enctype, not encrypt.
  2. You cannot send files using Ajax.BeginForm, at least not at easily as you would think. The quick solution would be to use Html.BeginForm() instead, but if this is unacceptable, then you probably need to look at a third-party plugin like for example Plupload.
Tobias
  • 2,811
  • 2
  • 20
  • 31
  • I changed it around to be Html.BeginForm and I changed it to say enctype but the file variable is still coming into my controller as null – Stc5097 Oct 04 '14 at 19:10
  • Thanks. :) I have some difficulties with replicating your problem, though. The code above works as it should for me. – Tobias Oct 04 '14 at 19:38
0

The null is because of "courseListViewModel" not because of the file,

you can set the break point and check it.

And I find you didn't use "courseListViewModel modelView" in your controller. Could it be deleted and retry it?

Sing
  • 3,942
  • 3
  • 29
  • 40
0

It looks as though your view model doesn't exist. If courseListViewModel were an actual model it would be blue similar to HttpPostedFileBase in your controller parameters. You might need to simply add a reference to the model class at the top of your controller.

Also, the model seems to be missing from the view, you would want to add a @using (yourmodels).courseListViewModel at the top of your view. If you just didn't paste all of the code, then forget this.

One more thing I would change is checking whether your file is null BEFORE you check the ContentLength. IF the file is null, it will throw an exception the way it is now.

HighlanderGrogg
  • 156
  • 2
  • 8