我想在javascript中获取模型的属性。所以我的 ajax 调用正在从 Controller 接收模型列表。在javascript中,成功后,我想获取模型的特定属性。下面是我的脚本:
$("#filter").keyup(function () {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: 'Search',
success:
function (result) {
console.log(result.Books);
var filter = $("#filter").val();
console.log(filter);
// Loop through each row of the table
result.Books.forEach(
(function (book, index) {
console.log(index);
var i = index;
var title = book[index];
// If the list item does not contain the text phrase fade it out
if ($(title).text().search(new RegExp(filter, "i")) < 0) {
$(book).fadeOut();
// Show the list item if the phrase matches and increase the count by 1
} else {
$(book).show();
}
})
)
}
});
});
正如你所看到的,我已经尝试过 var title=book[index]。我想要这样的东西:
var title = book[index].Title;
其中标题是模型的一个属性(此处为书本)。知道如何做到这一点吗?
最佳答案
只需使用book.Title
。
book
是 result.Books
集合的一个元素,因此您不需要使用 index
。
关于javascript - 如何在Javascript中获取模型的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434959/