When the user clicks on an Html.ActionLink, I need to call a controller method that will download a csv report for the user. I also need to pass this controller the values from two input boxes that will denote a start and end date range they're looking for.
Currently I can assign the Html.ActionLink parameters using jQuery, however they are not making it back to the controller. Both parameters in the controller method are instantiated with null values.
I also cannot use a form/submit method as that is already being used on this particular form to allow the users to see the data in the date range requested before exporting to the csv.
jQuery
$(document).ready(function() {
$('#startDate').change(function () {
$('a').attr('start', $(this).val());
});
$('#endDate').change(function () {
$('a').attr('end', $(this).val());
});
});
ASP MVC 3 View
@using (Html.BeginForm())
{
<div id="searchBox">
@Html.TextBox("startDate", ViewBag.StartDate as string, new { placeholder = " Start Date" })
@Html.TextBox("endDate", ViewBag.EndDate as string, new { placeholder = " End Date" })
<input type="image" src="@Url.Content("~/Content/Images/Search.bmp")" alt="Search" id="seachImage"/>
<a href="#" style="padding-left: 30px;"></a>
</div>
<br />
@Html.ActionLink("Export to Spreadsheet", "ExportToCsv", new { start = "" , end = ""} )
<span class="error">
@ViewBag.ErrorMessage
</span>
}
Controller Method
public void ExportToCsv(string start, string end)
{
var grid = new System.Web.UI.WebControls.GridView();
var banks = (from b in db.AgentTransmission
where b.RecordStatus.Equals("C") &&
b.WelcomeLetter
select b)
.AsEnumerable()
.Select(x => new
{
LastName = x.LastName,
FirstName = x.FirstName,
MiddleInitial = x.MiddleInitial,
EffectiveDate = x.EffectiveDate,
Status = x.displayStatus,
Email = x.Email,
Address1 = x.LocationStreet1,
Address2 = x.LocationStreet2,
City = x.LocationCity,
State = x.LocationState,
Zip = "'" + x.LocationZip,
CreatedOn = x.CreatedDate
});
grid.DataSource = banks.ToList();
grid.DataBind();
string style = @"<style> .textmode { mso-number-format:\@; } </style> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=WelcomeLetterOutput.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}