控制器的方法當用戶點擊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())
{
<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();
}
謝謝!我使用''@ Url.Action(「ExportToCsv」,「Agent」)'來獲取URL,但這正是我所期待的。謝謝! – NealR
很高興爲你效勞! –