如何从 MVC 转换获取 LIST 以用作 Json
How to Convert get LIST from MVC for using as Json
我尝试以 JSON 的形式从 MVC 控制器获取数据,但它似乎像 LIST 一样提交给我,我无法使用收到的 json 对象。除了使用 stringify。但我真的需要它的一部分而不是全部对象。
控制器***
public class testController : ApiController
{
public class Banditem
{
public int Id { get; set; }
public string Name { get; set; }
public string Detail { get; set; }
}
public class Bandlist
{
public List<Banditem> Bands { get; set; }
}
public class testAppType
{
public List<Banditem> Band { get; set; }
}
public testAppType Get()
{
using (var ms = new MySampleServiceClient(Wcf.Routing.RouterBindings.Local, Wcf.Routing.RouterAddresses.Local.RequestReply))
{
var objectToSerialize = new testAppType();
objectToSerialize.Band = new List<Banditem>
{
new Banditem { Id= 1, Name = "Test1", Detail = "111" },
new Banditem { Id= 2, Name = "Test2", Detail = "222" },
new Banditem { Id= 3, Name = "Test3", Detail = "333"},
new Banditem { Id= 4, Name = "Test4", Detail = "444"},
new Banditem { Id= 5, Name = "Test5", Detail = "555"}
};
return objectToSerialize;
}
}
}
}
HTML***
<script>
var url = "Band";
$(document).ready(function () {
// Send an AJAX request
$.getJSON(url)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#begin'));
});
});
});
function formatItem(item) {
return "ID: " + item.id + ":" + item.name + ': $' + item.price + "Category: " + item.category;
}
您需要更正 ajax 函数中 return 数据的实现,如下所示。
var url = "Band";
$(document).ready(function () {
// Send an AJAX request
$.getJSON(url)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data.Band, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#begin'));
});
});
});
function formatItem(item) {
return "ID: " + item.Id + ":" + item.Name ;
}
价格和类别不是 Band class 的属性。此外,属性名称区分大小写,因此您应该在 formatItem 函数中使用 Id 而不是 id。
我尝试以 JSON 的形式从 MVC 控制器获取数据,但它似乎像 LIST 一样提交给我,我无法使用收到的 json 对象。除了使用 stringify。但我真的需要它的一部分而不是全部对象。
控制器***
public class testController : ApiController
{
public class Banditem
{
public int Id { get; set; }
public string Name { get; set; }
public string Detail { get; set; }
}
public class Bandlist
{
public List<Banditem> Bands { get; set; }
}
public class testAppType
{
public List<Banditem> Band { get; set; }
}
public testAppType Get()
{
using (var ms = new MySampleServiceClient(Wcf.Routing.RouterBindings.Local, Wcf.Routing.RouterAddresses.Local.RequestReply))
{
var objectToSerialize = new testAppType();
objectToSerialize.Band = new List<Banditem>
{
new Banditem { Id= 1, Name = "Test1", Detail = "111" },
new Banditem { Id= 2, Name = "Test2", Detail = "222" },
new Banditem { Id= 3, Name = "Test3", Detail = "333"},
new Banditem { Id= 4, Name = "Test4", Detail = "444"},
new Banditem { Id= 5, Name = "Test5", Detail = "555"}
};
return objectToSerialize;
}
}
}
}
HTML***
<script>
var url = "Band";
$(document).ready(function () {
// Send an AJAX request
$.getJSON(url)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#begin'));
});
});
});
function formatItem(item) {
return "ID: " + item.id + ":" + item.name + ': $' + item.price + "Category: " + item.category;
}
您需要更正 ajax 函数中 return 数据的实现,如下所示。
var url = "Band";
$(document).ready(function () {
// Send an AJAX request
$.getJSON(url)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data.Band, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#begin'));
});
});
});
function formatItem(item) {
return "ID: " + item.Id + ":" + item.Name ;
}
价格和类别不是 Band class 的属性。此外,属性名称区分大小写,因此您应该在 formatItem 函数中使用 Id 而不是 id。