![](/img/trans.png)
[英]pass a javascript dictionary to an ASP.NET MVC action on controller
[英]Construct and pass JavaScript string list to ASP.NET MVC Controller action (not using ajax)
如何在JavaScript中构造字符串列表并将其传递给window.location
,以使其在围栏的C#侧显示为List<string>
?
(不使用Ajax POST,而是使用window.location
)
示例ASP.NET MVC控制器操作:
public FileResult GetMyCsvFile(List<string> columnNames)
{
...
}
JavaScript示例:
$('export-button').click(function (e) {
e.preventDefault();
// TODO: How do I construct the next line(s) such that it can pass a list of strings to my C# controller action?
window.location = ROOT_PATH + "Home/GetMyCsvFile ... ????
});
尝试
"Home/GetMyCsvFile?columnNames=firstColumn&columnNames=secondColumn...."
例如,创建<a>
标记并为其指定一个ID
@Html.ActionLink("Click me", "Test", "Task", null, new { id = "myLink" })
然后在脚本中(请注意,我已经使用了jquery,因为这就是您在问题中使用的方法)
$('#myLink').click(function (e) {
e.preventDefault();
// Define the values to pass to the controller
var firstCol = 'value1';
var secondCol = 'value2';
// Construct url with query string
var path = $(this).attr('href');
path += '?columnNames=' + firstCol + '&columnNames=' + secondCol;
// Navigate to the view
window.location = path;
});
如果它是一个可变的值列表,则需要在循环中构造网址,例如
for (var i = 0; i < myValues.length; i++) {
if (i === 0) {
path += '?columnNames=' + myValues[i];
} else {
path += '&columnNames=' + myValues[i];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.