title: how to deal with input type file when empty
body: <p>Am kind of new to mvc and i am trying to upload a file using <code>&lt;input type="file" name="file" id="file" /&gt;</code>. it is working when i attach a file to it via the browse button. but if i try saving the other form values when the file is empty, i get an error of Object reference not set to an instance of an object. I will be happy if someone can offer assistance.
  here is a part of my code.</p>
  
  <p>controller</p>
  
  <pre><code>public ActionResult InfoForm([Bind(Include = "Id, Title, Details, Day, Month, Year, Category, Status, ImageName ")] Info info, HttpPostedFileBase file)
          {
              if (ModelState.IsValid)
              {
                  if (file.ContentLength &gt; 0)
                  {
                      var fileName = Path.GetFileName(file.FileName);
                      string path = Server.MapPath("~") + "\\Content\\images\\info\\" + file.FileName;
                      file.SaveAs(path);
                      info.ImageName = fileName; // this line saves the name to the database.
                  }
  
  
                  db.Infoes.Add(info);
                  db.SaveChanges();
                  return RedirectToAction("InfoList");
              }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>...
  ...
  @Html.LabelFor(model =&gt; model.Category, new { @class = "control-label col-md-2" })
  @Html.DropDownListFor(  model =&gt; model.Category, new List&lt;SelectListItem&gt;
      {
          new SelectListItem { Text = "UpComing Event", Value = "UpComing Event" },
          new SelectListItem { Text = "What's New",  Value = "What's New"},
  
      }, new { @class = "form-control" } )
  
  @Html.ValidationMessageFor(model =&gt; model.Category)
  
  @Html.LabelFor(model =&gt; model.Status, new { @class = "control-label col-md-2" })
  @Html.DropDownListFor( model =&gt; model.Status, new List&lt;SelectListItem&gt;
                  {
                      new SelectListItem { Text = "Enable", Value = "True" },
                      new SelectListItem { Text = "Disable",  Value = "False"},
  
                  }, new { @class = "form-control" })
  @Html.ValidationMessageFor(model =&gt; model.Status)
  
  &lt;label class="control-label col-md-2"&gt; Upload Image&lt;/label&gt;
  &lt;input type="file" name="file" id="file" /&gt;
  @Html.ValidationMessageFor(model =&gt; model.ImageName)
  &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
  ...
  ...
  </code></pre>