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");
}