控制器的方法当用户点击Html.ActionLink,我需要调用一个控制器的方法,将下载该用户的csv报告。我还需要将这个控制器的值从两个输入框中传递出来,它们表示他们正在查找的开始日期和结束日期范围。传递输入参数,通过Html.ActionLink
目前我可以指定使用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())
{
@Html.TextBox("startDate", ViewBag.StartDate as string, new { placeholder = " Start Date" })
@Html.TextBox("endDate", ViewBag.EndDate as string, new { placeholder = " End Date" })
@Html.ActionLink("Export to Spreadsheet", "ExportToCsv", new { start = "" , end = ""})
@ViewBag.ErrorMessage
}
控制器方法
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 = @" ";
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();
}
2013-08-06
NealR