我想在html.Actionlink中传递输入类型的值,所以我尝试了下面的代码,但它没有工作
@Html.ActionLink("EXPORT TO EXCEL", "ExportToExcel", "UserManagement", new { @class = "btn btn-primary" },
new { ugid = $("selector").val() }
这是我的控制器
public void ExportToCSV(int ugid)
{
client.Open();
List<UserObjUserInfo> userlistbyUGID = client.GetUserListByUGID(ugid, "token").ToList();
client.Close();
StringWriter sw = new StringWriter();
sw.WriteLine("\"USER ID\",\"USERNAME\",\"NAME\",\"CREATE DATE\"");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Exported_Users.csv");
Response.ContentType = "text/csv";
foreach (var user in userlistbyUGID)
{
sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
user.ugid,
user.username,
user.name,
user.CreateDate));
}
Response.Write(sw.ToString());
Response.End();
}
答案 0 :(得分:3)
您将服务器端代码与客户端混淆。 ActionLink是一个在服务器上呈现的html助手,只是输出一个锚标记。在您的情况下,它产生以下内容:
<a href="UserManagement/ExportToExcel" class="btn btn-primary">EXPORT TO EXCEL</a>
在您的情况下,您需要客户端逻辑来生成正确的链接:
<a href="#" class="btn btn-primary" id="export-btn">EXPORT TO EXCEL</a>
<input type="text" id="ugid">
$(document).ready(function(){
$("#export-btn").click(function(event){
event.preventDefault();
window.location.href = "@Url.Action("ExportToExcel", "UserManagement")" + "?ugid=" + $("#ugid").val();
});
})
这是有效的fiddle
答案 1 :(得分:1)
这里的问题是您正在尝试将客户端值用于razer代码 你有两个选择
要么建立客户端链接而不是像
那样的actionlinkvar link ="/UserManagement/ExportToExcel?ugid="+$("selector").val();
或尝试这样的事情(如果你是持久的)
"@Html.ActionLink("EXPORT TO EXCEL", "ExportToExcel", "UserManagement", new { @class = "btn btn-primary" })"+"?Yourparam="+$("selector").val();
答案 2 :(得分:1)
如果您正在使用视图模型,只需更改htmlattribute和routevalues的位置即可
@Html.ActionLink("EXPORT TO EXCEL", "ExportToExcel", "UserManagement", new { ugid = item.value }, new { @class = "btn btn-primary" })
将为您提供以下网址
<a href="/UserManagement/ExportToExcel?ugid=whatever">EXPORT TO EXCEL</a>
订单事宜:
如果要动态生成routeattribute:
<a href="#" onclick="window.location.href='@Url.Action("ExportToExcel", "UserManagement")' + '?ugid=' + $('#ugid').val()">EXPORT TO EXCEL</a>
答案 3 :(得分:0)
您可以将ActionLink
更改为简单的html超链接标记
<a href="#" class="btn btn-primary" id="btnExport">EXPORT TO EXCEL</a>
为此Click
代码
a
个事件
$(document).on('click', '#btnExport',function(e){
e.preventDefault();
window.location.href = '@Url.Action("ExportToExcel", "UserManagement")' + '?ugid=' + $("selector").val();
})
创建一个新类,在其中添加一个方法。
namespace myProject.HelperClasses
{
public static class myExtensionClass
{
public static void WriteLineCSV(this StringWriter sw, params object[] Data)
{
StringBuilder sb = new StringBuilder();
foreach (var str in Data)
{
sb.Append(String.Format("\"{0}\",", str));
}
sw.WriteLine(sb);
}
}
}
在Controller
添加namespace
using myProject.HelperClasses
并在ExportToCSV
操作方法
public class UserManagement: Controller {
public ActionResult ExportToCSV(int ugid)
{
string FileName = "Exported_Users.csv";
string FilePath = Server.MapPath(Path.Combine("~/ExportFiles", FileName ));
client.Open();
List<UserObjUserInfo> userlistbyUGID = client.GetUserListByUGID(ugid, "token").ToList();
client.Close();
StringWriter sw = new StringWriter();
sw.WriteLineCSV("USER ID","USERNAME","NAME","CREATE DATE"); //you don't need to provide "\" every tim. ext. method WriteLineCSV will do it for you.
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Exported_Users.csv");
Response.ContentType = "text/csv";
foreach (var user in userlistbyUGID)
{
sw.WriteLineCSV(user.ugid,user.username,user.name, user.CreateDate);
}
Response.Write(sw.ToString());
Response.End();
return File(FilePath, "text/csv", FileName);
}
}
您可以在整个项目中使用扩展方法WriteLineCSV
,并且可以为其提供多个参数,因为它接受param数组。