[英]Pass Array from MVC Controller to Javascript
is it possible to read the contents of an array that is passed from an MVC Controller to JavaScript? 是否可以读取从MVC控制器传递给JavaScript的数组的内容?
This is a method in my controller that returns an array. 这是我控制器中的一个返回数组的方法。 (tried with a list before but didn't succeed)
(之前曾尝试过使用列表,但未成功)
public string[] GetAllEvents()
{
string[] array = new string[2];
array[0] = "a";
array[1] = "b";
List<string> lst = new List<string>();
lst.Add("a");
lst.Add("b");
return array;
}
Here is the JavaScript function from which I'm calling the Controller method. 这是我从中调用Controller方法的JavaScript函数。
function GetAllEvents() {
$.ajax({
type: "GET",
url: "/Service/GetAllEvents",
success: function (result) {
alert(result.toString() + " " + result[0]);
},
error: function (req, status, error) {
//alert("Error");
}
});
};
The result is a System.String[] and the result[0] is giving me 'S' as a result. 结果是System.String [],结果[0]给了我“ S”。
MVC actions should return ActionResult
s. MVC操作应返回
ActionResult
。
You can then return Json(list, JsonRequestBehavior.AllowGet)
and it will work. 然后,您可以
return Json(list, JsonRequestBehavior.AllowGet)
,它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.