title: How to change uploaded image on Razor page asp.net core
body: <p>I tried a file uploading method from StackOverflow and successfully uploaded the image with the <code>IHostEnvironment</code> setup. But I can't figure out the <code>Editmodel</code>. I want to delete the existing photo and add a new one in the edit form.</p>
  <p>Here is Model:</p>
  <pre><code>[Key]
      public int PostID { get; set; }
  [Required]
      [StringLength(160)]
      public string Title { get; set; }
  
      public string FeatureImage { get; set; }
  </code></pre>
  <p>Here is the Create.cshtml.cs:</p>
  <pre><code>public class CreateModel : PageModel
  {
      private readonly RazorApp.Data.ApplicationDbContext _context;
      private readonly IHostEnvironment hostingEnvironment;
      public CreateModel(RazorApp.Data.ApplicationDbContext context, IHostEnvironment environment)
      {
          this.hostingEnvironment = environment;
          _context = context;
      }
      [BindProperty]
      public Post Post { get; set; }
  
      [BindProperty]
      public IFormFile Image { get; set; }
  
      
      public async Task&lt;IActionResult&gt; OnPostAsync()
      {
          if (this.Image != null)
          {
              var fileName = GetUniqueName(this.Image.FileName);
              var uploads = Path.Combine(hostingEnvironment.ContentRootPath, &quot;wwwroot/uploads&quot;);
              var filePath = Path.Combine(uploads, fileName);
              this.Image.CopyTo(new FileStream(filePath, FileMode.Create));
              this.Post.FeatureImage = fileName; // Set the file name
          }
          var emptyPost = new Post();
          if (await TryUpdateModelAsync&lt;Post&gt;(
              emptyPost,
              &quot;post&quot;,
              p =&gt; p.Title, p =&gt; p.FeatureImage))
          {
              _context.Post.Add(Post);
              await _context.SaveChangesAsync();
              return RedirectToPage(&quot;./Index&quot;);
          }
  
          return Page();
      }
      private string GetUniqueName(string fileName)
      {
          fileName = Path.GetFileName(fileName);
          return Path.GetFileNameWithoutExtension(fileName)
                 + &quot;_&quot; + Guid.NewGuid().ToString().Substring(0, 4)
                 + Path.GetExtension(fileName);
      }
  }
  </code></pre>
  <p>As I said, uploading the image working fine. But I can't figure out for the edit.cshtml.cs. How can I delete the existing photo and add the new image?</p>