title: ASP.NET MVC Get File Upload name with user id and apply to separate field
body: <p>I have file download and upload working properly but I am trying to add user id to the beginning of the file name as well as apply the file name to a separate field.</p>
  
  <p>For Example:</p>
  
  <p>Upload File : (UserID)_FileName.docx</p>
  
  <p>FileName: (UserID)_FileName.docx</p>
  
  <p>The reason I want it like this is because I am uploading the file to the server instead of the DB but I want the DB to have record of the file name for the specific user who uploaded the file. </p>
  
  <p>In my controller Create() I have this snippet for the upload</p>
  
  <pre><code>if (file != null &amp;&amp; file.ContentLength &gt; 0)
                  {
                      var fileName = User.Identity.GetUserId() + "_" + Path.GetFileName(file.FileName);
                      var path = Path.Combine(Server.MapPath("~/UploadFiles"), fileName);
                      file.SaveAs(path);
                  }
  </code></pre>
  
  <p>In the cshtml I have the following two fields</p>
  
  <pre><code>&lt;div class="form-group"&gt;
              @Html.Label("Upload File", new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  &lt;input type="file" name="file" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.FileName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.TextBox("FileName", (object)ViewBag.FileName)
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>For the ViewBag.FileName in the controller I have:</p>
  
  <pre><code>ViewBag.Resume = Path.GetFileName(file.FileName);
  </code></pre>
  
  <p>In the end I actually will have the FileName field hidden or just handled during POST. So I am sure there is a different/better way of doing this.</p>