现在,我正在使用以下方法调用 Controller 方法:
<a class="k-button" href="@Url.Action("ExportData", "Home")">ExportData</a>
工作正常,但我还需要在 Controller 中发送两个字符串,从中获取 DateTime 并使用它们从列表中获取数据。 使用 Ajax 非常简单,但是我怎样才能用其他方式做到这一点呢? 我无法使用ajax的原因是因为Controller方法ExportData返回:
return File(csv, "text/csv", fileName);
出于某种原因,我无法使用 ajax 下载任何内容。 另外,通过使用 Url.Action,我可以发送一个参数,但不能发送更多参数。
编辑: 我还需要从以下位置获取这些字符串:
<input class="k-in" style="width: 150px; padding: 0; font-size: 0.9em;" type="text" id="DateFrom" name="DateFrom"/>
<input class="k-in" style="width: 150px; padding: 0; font-size: 0.9em;" type="text" id="DateTo" name="DateTo" />
最佳答案
您可以在 Action 方法中传递这样的路由值
<a class="k-button" href="@Url.Action("ExportData", "Home",new {p1="sa",p2="as"})">ExportData</a>
并在 ExportData 方法中取回这些数据,如下所示
public ActionResult ExportData(string p1,string p2)
{
...
}
根据您的要求,制作一个像这样的 JavaScript 函数
function test(){
var url=@Url.Action("ExportData", "Home");
var p1="12";//now you can set this value by javascript
var p2="123";
window.location.href = url + "?p1=" + p1 + "&p2=" + p2;//this will call you action method and pass you parameter.
}
现在您可以通过单击所需的按钮或 anchor 标记来调用此函数。
关于c# - 如何在不使用ajax的情况下将数据从View发送到Controller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203512/