Charles Okwuagwu
Charles Okwuagwu

Reputation: 10896

generate and download csv file by passing parameters to controller method in C# MVC

I have a method GenerateCSV(string accountNumber, string dt1, string dt2) in my ReportController

How do i initiate this request from my Report.cshtml?

 <h2>Settled Report</h2><br />
    @using (Html.BeginForm("Report", "ReportController", FormMethod.Get))
    {
        <p>
            Accounts: @Html.DropDownList("accountNumber")
            Start Date: @Html.TextBox("dt1", null, new { @class = "date-picker" })
            End Date: @Html.TextBox("dt2", null, new { @class = "date-picker" })
            @*@Html.Hidden("reportType", 1)*@
            <input type="hidden" id="" value="1" name="reportType" />
            <input type="submit" value="View Report" />
        </p>
        <div class="exportbuttons">
            @Html.ActionLink("Export to CSV", "GenerateCSV")
        </div>
    }

The GenerateCSV is called but all params are null, and the server tries to navigate to Report/GenerateCSV

Upvotes: 0

Views: 2630

Answers (2)

steve v
steve v

Reputation: 3540

Your ActionLink is just a link. It won't invoke the GenerateCSV action with the parameters you want because it is just a link with no parameters being passed.

UPDATE I'm not sure what the intent of your page is:

Is the intent that the user has two buttons, one to view the report, one to export? If so, refer to Option 2 below.

Is the intent that the user has one button and all it does is export to CSV? If so, refer to Option 1 below.

Option 1 - Only one button on the page

Change your Razor as follows:

    @using (Html.BeginForm("GenerateCSV", "Report", FormMethod.Get))
    {
        <p>
            Accounts: @Html.DropDownList("accountNumber")
            Start Date: @Html.TextBox("dt1", null, new { @class = "date-picker" })
            End Date: @Html.TextBox("dt2", null, new { @class = "date-picker" })
            @*@Html.Hidden("reportType", 1)*@
            <input type="hidden" id="" value="1" name="reportType" />
            <input name="reportType" type="submit" value="Export to CSV" />
        </p>
    }

Option 2 - Two buttons on the page

If you want to allow the user to either click the View Report button to view it in a browser or Export to CSV to have the report exported instead of shown to the user, I would suggest:

Change your Report() method so that it accepts a string parameter called reportType, and check the value to determine whether to show it in a browser or call GenerateCSV()

public ActionResult Report(string accountNumber, string dt1, string dt2, string reportType)
{
   if (reportType == "View Report")
   {
      // code to show report to user in browser
   }
   else 
   {
      return GenerateCSV(accountNumber, dt1, dt2);
   }
}

Change your razor as follows:

    @using (Html.BeginForm("Report", "ReportController", FormMethod.Get))
    {
        <p>
            Accounts: @Html.DropDownList("accountNumber")
            Start Date: @Html.TextBox("dt1", null, new { @class = "date-picker" })
            End Date: @Html.TextBox("dt2", null, new { @class = "date-picker" })
            @*@Html.Hidden("reportType", 1)*@
            <input type="hidden" id="" value="1" name="reportType" />
            <input name="reportType" type="submit" value="View Report" />
            <input name="reportType" type="submit" value="Export to CSV" />
        </p>
    }

Upvotes: 1

NZeta520
NZeta520

Reputation: 114

Your model should be like

public class CSVDetails
{
    public List<long> AccountNumber {get; set;}
    public DateTime dt1 {get; set;}
    public DateTime dt2 {get; set;}
}

Then your view should be like

@model YourProjectNamespace.Models.CSVDetails
<h2>Settled Report</h2><br />
@using (Html.BeginForm("Report", "ReportController", FormMethod.Get))
{
    <p>
        Accounts: @Html.DropDownListFor(a=>a.AccountNumber)
        Start Date: @Html.TextBoxFor(a=>a.dt1, new { @class = "date-picker" })
        End Date: @Html.TextBoxFor(a=>a.dt2, new { @class = "date-picker" })

        <input type="hidden" id="" value="1" name="reportType" />
        <input type="submit" value="View Report" />
    </p>
    <div class="exportbuttons">
        @Html.ActionLink("Export to CSV", "GenerateCSV")
    </div>

In controller

public ActionResult GenerateCSV(CSVDetais csv)
{
      // your logic here
}

Hope it helps you!!!

Upvotes: 0

Related Questions