title: uploading image with ajax MVC
body: <p>I'm having issue creating new object into my database when I'm trying to include image within it.</p>
  
  <p>My model:</p>
  
  <pre><code>    public Dog()
  
      {
  
      public int Id { get; set; }
      public string Name { get; set; }
      public string DogImageUrl { get; set; }
  
      }
  </code></pre>
  
  <p>my HTML code:</p>
  
  <pre><code> @if (User.Identity.IsAuthenticated)
      {
            &lt;h7&gt;New Dog&lt;/h7&gt;
              &lt;input id="dogName" type="text" placeholder="dog name" /&gt;
  
                  &lt;input type="file" name="file" style="width: 100%;" /&gt;
  
                  &lt;input id="createNewDog" type="button" value="Go" /&gt;
  
  
              &lt;/div&gt;
          &lt;/div&gt;
      }
  </code></pre>
  
  <p>My Ajax post code:
                $('#addNewDog').click(function (e) {</p>
  
  <pre><code>         var imgToUpload = $("input[name=file]").get(0).files[0];
           var data = {
                  "Name": $('#dogName').val(),
                  "DogImageUrl ": "nullForNow",
  
  
              };
              var url = "@Url.Action("NewDog", "Home")";
              $.ajax({
                  url: url,
                  data: { model: data, file: Request.File[imgToUpload] },
                  cache: false,
                  type: "POST",
                  contentType: "multipart/form-data",
                  processData: false,
                  success: function (respone) {
                      alert(respone);
                      location.reload();
                  },
                  error: function (reponse) {
                      alert(reponse);
                  }
              });
              e.stopPropagation();
          });
  </code></pre>
  
  <p>my controller:</p>
  
  <pre><code> public ActionResult NewDog(Dog model, HttpPostedFileBase file)
      {
  
  
          using (var contex = new DogContext())
          {
              if (User.Identity.IsAuthenticated)
              {
  
                  if (file != null)
                  {
                      try
                      {
                          if (file.ContentType == "image/jpeg")
                          {
                              if (file.ContentLength &lt; 500000)
                              {
                                  string pic = System.IO.Path.GetFileName(file.FileName);
                                  string path = System.IO.Path.Combine(
                                                         Server.MapPath("~/Content/GroupImages"), pic);
                                  // file is uploaded
                                  file.SaveAs(path);
                                  model.DogImageUrl = path;
                              }
                          }
                      }
                      catch (Exception ex)
                      {
                          return Content(ex.ToString());
                      }
                  }
  
                  contex.Dogs.Add(model);
                  contex.SaveChanges();
              }
          }
  </code></pre>
  
  <p>My img always comes null.. I searched all over the internet and didn't figure it out. 
  the goal is to save the PATH to my image in my database.</p>