0

I have a Model as:

 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 string file_one { get; set; }
    [NotMapped]
    public HttpPostedFileBase file1 { get; set; }
}

And a controller as:

 public ActionResult Index()
    {
        return View();
    }

    public ActionResult Request(Events e)
    {

        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 full Razor View for Index:

 @model InsertFromdifferentControllers.Models.Events

@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>
}

When i click on request button it gives an error as: Object Reference Not set to an instance of an object and it Marks the following line in red color:

string filename = Path.GetFileNameWithoutExtension(e.file1.FileName);

I would like to add that if i remove the button then everything saves properly and it seems that the issue is only with the upload button feature.

John Kamaal
  • 129
  • 8
  • 1
    You are calling [this overload](https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.formextensions.beginform?view=aspnet-mvc-5.2#System_Web_Mvc_Html_FormExtensions_BeginForm_System_Web_Mvc_HtmlHelper_System_String_System_String_System_Object_) of `BeginForm`, so your `enctype = "multipart/form-data"` goes into the route values, not into form attributes. You need to use [this overload](https://stackoverflow.com/a/21998688/11683). – GSerg Nov 12 '18 at 08:54
  • @GSerg could you please be a little more specific – John Kamaal Nov 12 '18 at 08:58
  • 1
    `@using (Html.BeginForm("Request", "Requester", FormMethod.Post, new { enctype = "multipart/form-data" }))` –  Nov 12 '18 at 08:59
  • @JohnKamaal, use above comment code by Stephen. I think its proper solution – er-sho Nov 12 '18 at 09:01
  • @StephenMuecke thanks a lot, now there is another small issue, can you please check the UPDATES section of my question. – John Kamaal Nov 13 '18 at 13:28
  • @ershoaib Thank you for your time, although there is another minor issue, can you please check the UPDATES section of my question. – John Kamaal Nov 13 '18 at 13:29
  • @JohnKamaal, now i leave my office. so I'll look it tomorrow :) – er-sho Nov 13 '18 at 13:39
  • 1
    If you have a new question then you need to ask a new question, not alter the existing one (I have rolled back your edit) –  Nov 13 '18 at 22:38
  • @ershoaib if you are available, https://stackoverflow.com/questions/53300342/can-not-submit-the-form-if-one-of-the-upload-buttons-out-of-two-is-not-having-an – John Kamaal Nov 14 '18 at 13:01

0 Answers0