[英]asp.net mvc3 return multiple json list
Im asp.net mvc3 c# code returns json list like this: 我在asp.net mvc3 c#代码返回json列表,如下所示:
return Json(new { name = UserNames, imageUrl = ImageUrls });
UserNames
and ImageUrls
are both List<string>
types UserNames
和ImageUrls
都是List<string>
类型
And this is my javascript 这是我的JavaScript
function StartSearch(text) {
$.ajax({
url: '/Shared/Search',
type: 'POST',
data: { SearchText: text },
dataType: 'json',
success: function (result) {
$.each(result, function (i, item) {
alert(result[i].name);
});
}
});
}
How I can get names and ImageUrls
? 我怎么能得到名字和
ImageUrls
?
Thanks 谢谢
Access name as a property of result like this result.name[i]
访问名称作为结果的属性,如
result.name[i]
Essentially, result will contain name and imageUrl which are both arrays, just like you have defined in your anonymous type, so your code should be modified like this to display an alert for each name in the name array 本质上,结果将包含name和imageUrl,它们都是数组,就像你在匿名类型中定义的一样,所以你的代码应该像这样修改,以显示名称数组中每个名称的警报
function StartSearch(text) {
$.ajax({
url: '/Shared/Search',
type: 'POST',
data: { SearchText: text },
dataType: 'json',
success: function (result) {
$.each(result.name, function (i, item) {
alert(item);
});
}
});
}
as $each
iterates over the items in the name array, it will pass the item to the second parameter of your call back, ie item. 当
$each
遍历名称数组中的项目时,它会将项目传递给回调的第二个参数,即item。
so 所以
$.each(result.name, function (i, item) {
alert(item);
});
will popup each name. 将弹出每个名称。
Notes: 笔记:
You may want to change the properties on your anonymous type to reflect that they are a collection: 您可能希望更改匿名类型的属性以反映它们是一个集合:
return Json(new { UserNames = UserNames, ImageUrls = ImageUrls });
this way it will make more sense when you iterate over them in your success function. 这样,当你在成功函数中迭代它们时,它会更有意义。
As AlfalfaStrange pointed out, I didn't demonstrate how you might access both arrays. 正如AlfalfaStrange指出的那样,我没有演示如何访问这两个数组。 Which made me think, what is the relationship between user names and image urls?
这让我想到,用户名和图片网址之间的关系是什么?
Is this a list of images for a user? 这是用户的图像列表吗? Maybe what you should consider is creating a specific model for this.
也许您应该考虑为此创建一个特定的模型。 For instance, UserDisplayModel:
例如,UserDisplayModel:
public class UserDisplayModel
{
public string UserName {get;set;}
public string ImageUrl {get;set;}
}
In your controller return a list of UserDisplayModels. 在您的控制器中返回UserDisplayModels列表。 If this is the case, you'll have to rethink why they are two separate lists in the first place.
如果是这种情况,您必须首先重新考虑它们为什么是两个单独的列表。 Maybe ImageUrl should be a field on the User table.
也许ImageUrl应该是User表上的一个字段。
So now, when you're returning a single list, eg 所以现在,当你返回一个列表时,例如
List<UserDisplayModel> users = //get users from db
return Json(new { Users = Users});
you can iterate them in one go in js code: 你可以在js代码中一次性迭代它们:
$.each(result.Users, function (i, item) {
alert(item.Name);
alert(item.ImageUrl);
});
This alerts the value of Name
from each record of each list. 这会从每个列表的每个记录中警告
Name
的值。
$.each(result, function (i, item) {
for (var x = 0; x < result.FirstList.length; x++) {
alert(result.FirstList[x].Name);
alert(result.SecondList[x].Name);
}
});
This assumes your Json response if formed correctly. 如果正确形成,则假定您的Json响应。 Like this:
像这样:
return Json(new { FirstList = results, SecondList = otherResults }, JsonRequestBehavior.AllowGet);
But as a side note, I see other problems with your code that you need to address 但作为旁注,我发现您需要解决的代码存在其他问题
POST
, you're searching based on input. POST
,你正在根据输入进行搜索。 Change POST
to GET
in your Ajax call POST
更改为GET
JsonResult
. JsonResult
。 Naming conventions for C# method parameters call for Pascal-casing. C#方法参数的命名约定调用Pascal-casing。 Use a lowercase letter for first character
第一个字符使用小写字母
public JsonResult Search(string searchText) { .... return Json(new { name = UserNames, imageUrl = ImageUrls }, JsonRequestBehavior.AllowGet); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.