0

Sorry I am quite new in ASP.NET MVC and I have this piece of code in the Home view folder (file Index.cshtml):

@{
    ViewBag.Title = "Home Page";
}


<div class="row">
    <div class="col-md-4">
        <h2>Select file</h2>
        <p>
            <input id="File1" type="file" />
        </p>
        <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Upload to SQL Server</h2>
        <input type="button" title="Upload to SQL Server" value="Upload to SQL Server" onclick="location.href='@Url.Action("Upload2SS", "SystemLogs")'" />
    </div>
</div>

When I click the "Upload to SQL Server" button, I simply want to pass the File1 value to the controller action:

public RedirectToRouteResult Upload2SS(FormCollection form)
{
    string filePath = form["File1"].ToString();
    var data = GetDataTabletFromCSVFile(filePath);
    return RedirectToAction("Index");
}

However, I keep on getting a System.NullReferenceException for the filePath variable; can anyone tell me what I am missing please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hello, you must set the property "name" in the "input". This works for all controls in MVC. – Jsperk Mar 15 '20 at 16:52

1 Answers1

1

Your code missing a lot of things. First of all you need to use a form tag in which you will have input file and input button.

This form will help you to submit bunch of information to the server. Similarly you need to set the name attribute, From your code you are missing name attribute in your input file.

You can search Stackoverflow and find any answer. For submitting file, read this answer https://stackoverflow.com/a/28380690/713789

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79