사용자가 Html.ActionLink
을 클릭하면 사용자에게 csv
보고서를 다운로드 할 컨트롤러 메소드를 호출해야합니다. 또한이 컨트롤러에 두 개의 입력란의 값을 전달해야합니다.이 상자는 찾고있는 시작일과 종료일 범위를 나타냅니다.Html.ActionLink를 통해 컨트롤러 메소드에 입력 매개 변수를 전달하십시오.
현재는 Html.ActionLink
매개 변수를 jQuery를 사용하여 할당 할 수 있지만 컨트롤러로 되돌릴 수는 없습니다. 컨트롤러 메서드의 두 매개 변수는 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을 얻기 위해''@ Url.Action ("ExportToCsv", "Agent") ''를 사용 했었지만 이것은 내가 찾고 있던 것과 정확히 일치했다. 고맙습니다! – NealR
다행 당신을 위해 일했다! –