c#给html传参数,c# - 通过Html.ActionLink将输入参数传递给控制器​​方法 - 堆栈内存溢出...

博客探讨了在ASP.NET MVC3中如何在用户点击Html.ActionLink时调用控制器方法来下载CSV报告,并传递两个日期参数。当前遇到的问题是jQuery能够设置ActionLink的参数,但这些参数并未成功传递到控制器。由于已存在用于查看数据的表单提交,因此不能使用常规表单提交方式。示例代码展示了尝试使用jQuery监听日期输入框变化并更新ActionLink属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当用户单击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())

{

@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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值