title: upload image in registration form in asp mvc
body: <p>I want to save my model in database and upload my picture to server I want to do it all when I hit the save button</p>
  
  <p>My view model is:</p>
  
  <pre><code>public class PostViewModel
  {
      public int PostId { get; set; }
  
      [Required]
      [StringLength(255)]
      public String Address { get; set; }
  
      [Required]
      [StringLength(255)]
      [Display(Name = "title")]
      public string PostTitle { get; set; }
  
      [Required]
      [Display(Name = "full text")]
      public string PostContent { get; set; }
  
      [StringLength(30)]
      [Display(Name = "Image")]
      public string PostImage { get; set; }
  
      [Display(Name = "Date")]
      public DateTime CreatedAt { get; set; }
  
      public virtual IList&lt;TagCheckBox&gt; TagList { get; set; }
  
  }
  </code></pre>
  
  <p>and Create.cshtml :</p>
  
  <pre><code>@model First.Areas.Admin.ViewModels.PostViewModel
  @using (Html.BeginForm("Create", "Posts", FormMethod.Post, new { enctype = "multipart/form-data" }))   {   @Html.AntiForgeryToken()    
  &lt;div class="row"&gt;
      &lt;div class="col-lg-8"&gt;
          &lt;div class="form-horizontal"&gt;
  
              @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.TextBoxFor(model =&gt; model.PostTitle, new { @class = "form-control" })
                      @Html.ValidationMessageFor(model =&gt; model.PostTitle, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Address, "لینک ", htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.TextBoxFor(model =&gt; model.Address, new { @class = "form-control", data_slug = "#PostTitle" })
                      @Html.ValidationMessageFor(model =&gt; model.Address, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.PostContent, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.TextAreaFor(model =&gt; model.PostContent, new { htmlAttributes = new { @class = "form-control" } })
                      @Html.ValidationMessageFor(model =&gt; model.PostContent, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.PostImage, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                    &lt;input type="file" name="fileUpload" /&gt;
                      @Html.ValidationMessageFor(model =&gt; model.PostImage, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;div class="col-md-offset-2 col-md-10"&gt;
                      &lt;input type="submit" value="save" class="btn btn-primary" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>and controller :</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Create([Bind(Include = "PostId,Address,PostTitle,PostContent,PostImage,TagList")] PostViewModel postViewModel,HttpPostedFileBase file)
      {
          if (ModelState.IsValid)
          {
              string fileName = "1.jpg";
              if (file != null &amp;&amp; file.ContentLength &gt; 0)
              {
                  fileName = Path.GetFileName(file.FileName);
                  var path = Path.Combine(Server.MapPath("~/Content/images/blog"), fileName);
                  file.SaveAs(path);
              }
              else
              {
                  return View(postViewModel);
              }
              var selectedTags = ReconsileTags(postViewModel.TagList);
  
              Post post = new Post
              {
                  PostTitle = postViewModel.PostTitle,
                  PostContent = postViewModel.PostContent,
  
                  Address = postViewModel.Address,
                  Tags = selectedTags
              };
              post.PostImage = fileName ;
              post.AuthorId = User.Identity.GetUserId();
  
              db.Posts.Add(post);
              await db.SaveChangesAsync();
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>the file (HttpPostedFileBase) is null !? 
  please help me to solve this. (I'm newbie in asp mvc )</p>