title: handle exception when @html.beginform is sent with empty fields in asp mvc 4
body: <p>I'm getting some exceptions when sending form with empty fields to controller. Actually I'm not using typed view I just create a form that send data to controller through <code>@Html.BeginForm</code>. When I don't enter "dateParution" I'm getting an ArgumentException which says that dateParution can not get null value , and when I don't enter file I'm getting an NullReferenceException. What I want is to show user a message to tell that all fields should be entered and avoid the exception page.</p>
  
  <p>Here is my view: </p>
  
  <pre><code>@using (Html.BeginForm("Form", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
                 {     
  &lt;table cellpadding="2" cellspacing="10"&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;
                                      Choisir journal :
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      @Html.DropDownList("IdJournal")
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;
                                      Numéro de l'édition:
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;input type="text" name="numEditionJournal" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;
                                      Date de parution:
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;input class="form-control span2" name="dateParution" size="16" type="date" value="12-02-2014" &gt;
                                      &lt;/td&gt;
                              &lt;/tr&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;
                                      Choisirr image:
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;input type="file" multiple name="files" id="upload"/&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/table&gt;
  
  
                  &lt;/div&gt;
  
  
                       &lt;button type="submit" id="load" class="btn btn-primary"&gt;Confirmer&lt;/button&gt;
  
                 }
  </code></pre>
  
  <p>and my controller </p>
  
  <pre><code>[HttpPost]
      public ActionResult Form(List&lt;HttpPostedFileBase&gt; files, DateTime dateParution, long IdJournal, string numEditionJournal)
      {
  
  
          ScanITAPP.Service.ImageRender service = new Service.ImageRender();
          /* service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);*/
          try
          {
              if (files != null &amp;&amp; dateParution != null &amp;&amp; numEditionJournal != null)
              {
                  foreach (HttpPostedFileBase file in files)
                  {
  
                      byte[] data;
                      using (Stream inputStream = file.InputStream)
                      {
                          MemoryStream memoryStream = inputStream as MemoryStream;
                          if (memoryStream == null)
                          {
                              memoryStream = new MemoryStream();
                              inputStream.CopyTo(memoryStream);
                          }
                          data = memoryStream.ToArray();
                      }
  
                      string extension = System.IO.Path.GetExtension(file.FileName);
                      string filename = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
  
                      if (extension == ".pdf")
                      {
  
                          using (var image = new MagickImage())
                          {
                              image.Quality = 300;
                              image.Density = new MagickGeometry(144, 144);
                              image.Read(data);
  
  
                              image.Write("C:\\AnnoncesImages\\" + filename + ".jpg");
                              byte[] bytes = System.IO.File.ReadAllBytes("C:\\AnnoncesImages\\" + filename + ".jpg");
  
                              AnnonceVImgSet annImg = new AnnonceVImgSet();
                              annImg.Id = 1;
  
                              ImgOrgSet img = new ImgOrgSet();
  
                              img.User_Id = 1;
                              img.Journal_Id = IdJournal;
                              img.dateModif = DateTime.Now;
                              img.dateParution = dateParution;
                              img.dateSaisi = DateTime.Now;
                              img.numEditionJournal = numEditionJournal;
  
                              img.image = bytes;
                              using (Bd_scanitEntities dbContext = new Bd_scanitEntities())
                              {
                                  dbContext.ImgOrgSet.Add(img);
                                  dbContext.SaveChanges();
                              }
                              TempData["Message"] = "Image PDF ajoutée ";
                          }
                      }
                      else
                      {
                          service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);
                          TempData["Message"] = "Image ajoutée ";
                      }
                  }
                  return RedirectToAction("Index");
              }
              else
              {
                  ViewBag.RenderedAlert = "Vous n'avez pas rempli tous les champs";
                  TempData["Message"] = "Image non ajoutée if";
              }
  
  
  
          }
          catch (ArgumentException)
          {
              ViewBag.RenderedAlert = "Vous n'avez pas rempli tous les champs";
              TempData["Message"] = "Image ajoutée catch";
              return View("Index");
          }
          return View("Index");
      }
  </code></pre>