当用户单击 时Html.ActionLink
,我需要调用一个控制器方法,该方法将为csv
用户下载报告。我还需要将两个输入框中的值传递给这个控制器,这两个输入框将表示他们正在寻找的开始和结束日期范围。
目前我可以使用 jQuery 分配Html.ActionLink
参数,但是它们没有返回到控制器。null
控制器方法中的两个参数都用值实例化。
我也不能使用表单/提交方法,因为该特定表单已使用该方法,以允许用户在导出到 csv 之前查看请求的日期范围内的数据。
jQuery
$(document).ready(function() {
$('#startDate').change(function () {
$('a').attr('start', $(this).val());
});
$('#endDate').change(function () {
$('a').attr('end', $(this).val());
});
});
ASP MVC 3 视图
@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>
}
控制器方法
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();
}