I have a basic asp.net c# application which has a form to submit some data to database, This form has an upload button to upload a file.
Initially, there was a problem that: i was not able to submit a form without uploading a file, it was giving an error as [object reference not set to an instance of an object], which means uploading a file was a must before submitting the form, So to resolve that issue i made some changes in my controller.
Now it submits the form even if i don't upload anything, but the new problem is that when i upload a file and submit then it still submits the form successfully but it does not upload the actual file.
This is the model:
  public class Events
{
    public int Id { get; set; }
    public string title { get; set; }
    public string amount { get; set; }
    public string Finance_Approval { get; set; }
    public DateTime date_time { get; set; }
    public string file_one { get; set; }
    [NotMapped]
    public HttpPostedFileBase file1 { get; set; }
}
This is the controller:
    public ActionResult Index()
    {  
        return View();
    }
   public ActionResult Request(Events e)
    {
        if (e.file_one==null)
        {
            _context.evt.Add(e);
            _context.SaveChanges();
            return Content("Added");
        }
        else
        {
            string filename = Path.GetFileNameWithoutExtension(e.file1.FileName);
            string extension = Path.GetExtension(e.file1.FileName);
            filename = filename + DateTime.Now.ToString("yymmssfff") + extension;
            e.file_one = "PM_Files/" + filename;
            filename = Path.Combine(Server.MapPath("~/PM_Files/"), filename);
            e.file1.SaveAs(filename);
            _context.evt.Add(e);
            _context.SaveChanges();
            return Content("Added");
        }
    }
And this is the razor view:
@using (Html.BeginForm("Request", "Requester", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.LabelFor(a => a.title)
        @Html.TextBoxFor(a => a.title, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(a => a.amount)
        @Html.TextBoxFor(a => a.amount, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Select the file word or pdf etc</label>
        <input type="file" name="file1" />
    </div>    
    <button class="btn btn-primary">Request</button>
}
 
     
    