我有这样的json数组:
{"error":false,
"message":"",
"Album_list":[{"Album":{"id":"27","user_id":"46","title":"Internal","status":"1","time":"","created":"2016-05-02 17:20:27","modified":"2016-05-02 17:20:27"}},
{"Album":{"id":"30","user_id":"46","title":"efdrhty","status":"1","time":"","created":"2016-05-04 08:52:12","modified":"2016-05-04 08:52:12"}},
{"Album":{"id":"37","user_id":"46","title":"external","status":"1","time":"","created":"2016-05-06 08:04:55","modified":"2016-05-06 08:04:55"}},
{"Album":{"id":"38","user_id":"46","title":"James Jobs" ,"status":"1","time":"","created":"2016-05-17 09:40:41","modified":"2016-05-17 09:40:41"}},
{"Album":{"id":"41","user_id":"46","title":"17th May 2016","status":"1","time":"","created":"2016-05-17 10:20 :10","modified":"2016-05-17 10:20:10"}}]}
现在,在success
函数中,我需要循环中的这个Album_list
数据。
我试试这个,但我无法正确使用
Javascript代码:
success: function (response) {
if (response.error == true) {
console.log(response.message);
} else {
content += '<div class="ui-grid-b gallery-sec">';
for (img in response.Album_list) {
console.log(img);
content += '<div class="ui-block-b gallery-sec">' +
'<div class="gallery-thumb-col">' +
'<div class="gallery-thumb">' +
'<img src="images/blue-box-logo.png" alt="">' +
'</div>' +
'<div class="full">' +
'<h2>'+img.Album.title+'</h2>' + //I need the title of album here
'</div>' +
'<p>2 Photos</p>' +
'<div class="ftr-bg">' +
'<i class="fa fa-trash-o" aria-hidden="true"></i></div>' +
'</div>' +
'</div>';
}
content += '</div>';
$('#gallery .page-content').html(content);
}
}
console.log(img);
得0,然后是1,然后是4,但我需要Album
数组。
答案 0 :(得分:2)
使用此:
public ActionResult Index() //Wait, got an exception, cannot use more than one 'Index()'
{
bindCombo();
return this.View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file) //Same problem, how do i fix?
{
ModelVariables mv = new ModelVariables();
if (file != null && file.ContentLength > 0)
{
mv.fileSubmit = Path.GetFileName(file.FileName);
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index", mv);
}
Documentation about for in loop
因为我看到你正在使用jQuery。你可以这样做:
for (var i = 0; i < response.Album_list.length; i++) {
var album = response.Album_list[i];
console.log(album);
}
答案 1 :(得分:1)
替换
for (img in response.Album_list) {
// code here
}
与
response.Album_list.forEach(function (img) {
// code here
});
因为img
不是元素,而是索引的变量。
答案 2 :(得分:0)
您也可以尝试:
for (var index in response.Album_list) {
img = response.Album_list[index];
// rest of the code as it it
}