title: How to insert upload file data and form data from view to database in MVC
body: <p>I have a create function to insert view data to database. The view has a upload file textbox along with other textboxes. How do I bind form data and insert the uploaded file in bytes to a table?</p>
  
  <pre><code>@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
      @Html.LabelFor(model =&gt; model.Princial_name)
      @Html.EditorFor(model =&gt; model.Princial_name)
      @Html.ValidationMessageFor(model =&gt; model.Princial_name)
  
      @Html.LabelFor(model =&gt; model.p_campus)
      @Html.EditorFor(model =&gt; model.p_campus)
      @Html.ValidationMessageFor(model =&gt; model.p_campus)
  
      @Html.LabelFor(model =&gt; model.filepath)
      &lt;input type="file" name="postedFile" /&gt;
  
      &lt;input type="submit" value="Submit" class="btn btn-default" /&gt;
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create([Bind(Include = "id,Princial_name,p_campus,filepath,filedata")] grant grant)
  {
      HttpPostedFileBase postedFile;
      byte[] bytes;
      using (BinaryReader br = new BinaryReader(postedFile.InputStream))
      {
          bytes = br.ReadBytes(postedFile.ContentLength);
      }
      if (ModelState.IsValid)
      {
          grant.id = Guid.NewGuid();
           db.Entry(grant).State = EntityState.Modified;
          //add filepath
          db.Entry(grant).Property("filepath").CurrentValue =  Path.GetFileName(postedFile.FileName);
          db.Entry(grant).Property("filedata").CurrentValue = bytes;
          db.grant.Add(grant);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
       return View(grant);
  }
  </code></pre>
  
  <p>The controller cannot insert both form data and upload file data to database. How do I do it? Thanks.</p>