Asp.net mvc 5 使用$.GetJSON()函数检索类对象数组
我已经实现了一个WebAPI2控制器,它返回一个类对象数组。现在,我想在我的MVC5应用程序中使用这个API。为此,我发现可以使用getJSON方法。但问题是我不知道如何从getJSON方法获取类对象数组。还取决于返回的对象数量,我必须在UI上创建相同数量的网格。欢迎使用任何指针、链接或示例代码。我不知道这是否是您要查找的内容,但在解析包含多个对象的json字符串时,您只需对每个对象使用。 在循环中,您可以创建网格,因此取决于对象的数量。Asp.net mvc 5 使用$.GetJSON()函数检索类对象数组,asp.net-mvc-5,asp.net-ajax,getjson,asp.net-web-api2,Asp.net Mvc 5,Asp.net Ajax,Getjson,Asp.net Web Api2,我已经实现了一个WebAPI2控制器,它返回一个类对象数组。现在,我想在我的MVC5应用程序中使用这个API。为此,我发现可以使用getJSON方法。但问题是我不知道如何从getJSON方法获取类对象数组。还取决于返回的对象数量,我必须在UI上创建相同数量的网格。欢迎使用任何指针、链接或示例代码。我不知道这是否是您要查找的内容,但在解析包含多个对象的json字符串时,您只需对每个对象使用。 在循环中,您可以创建网格,因此取决于对象的数量。 $.each(jsonstring, function
$.each(jsonstring, function (i, object) {
/*create your grid and handle your object here*/
/*access attributes of your object with object.attributname*/
}
这就是你要找的吗 我不知道这是否是您想要的,但是在解析包含多个对象的json字符串时,您可以简单地对每个对象使用。 在循环中,您可以创建网格,因此取决于对象的数量。
$.each(jsonstring, function (i, object) {
/*create your grid and handle your object here*/
/*access attributes of your object with object.attributname*/
}
这就是你要找的吗 下面的示例将帮助您使用web api并在页面中使用该数据 控制器:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
公共类TestController:ApiController
{
公共IEnumerable Get()
{
var context=newtest();
返回context.temps;
}
}
数据库的模型和DbContext类:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
[表格(“临时”)]
公共类临时工
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
[列(TypeName=“numeric”)]
公共十进制tempid{get;set;}
[必需]
[长度(50)]
公共字符串名称{get;set;}
[必需]
[长度(50)]
公共字符串地址{get;set;}
}
公共类测试:DbContext
{
公开考试()
:base(“名称=测试”)
{
}
公共虚拟数据库集temps{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(e=>e.tempid)
.HasPrecision(18,0);
}
}
视图侧:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
$(文档).ready(函数()
{
$.getJSON(“http://localhost:51197/api/Test,函数(数据){
$。每个(数据、函数(键、值){
$(“”+val.name+“”+val.address
+)。附于(“#tbPerson”);
});
});
});
下面的示例将帮助您使用web api并在页面中使用该数据
控制器:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
公共类TestController:ApiController
{
公共IEnumerable Get()
{
var context=newtest();
返回context.temps;
}
}
数据库的模型和DbContext类:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
[表格(“临时”)]
公共类临时工
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
[列(TypeName=“numeric”)]
公共十进制tempid{get;set;}
[必需]
[长度(50)]
公共字符串名称{get;set;}
[必需]
[长度(50)]
公共字符串地址{get;set;}
}
公共类测试:DbContext
{
公开考试()
:base(“名称=测试”)
{
}
公共虚拟数据库集temps{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(e=>e.tempid)
.HasPrecision(18,0);
}
}
视图侧:-
public class TestController : ApiController
{
public IEnumerable<temp> Get()
{
var context = new Test();
return context.temps;
}
}
[Table("temp")]
public class temp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "numeric")]
public decimal tempid { get; set; }
[Required]
[StringLength(50)]
public string name { get; set; }
[Required]
[StringLength(50)]
public string address { get; set; }
}
public class Test : DbContext
{
public Test()
: base("name=Test")
{
}
public virtual DbSet<temp> temps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<temp>()
.Property(e => e.tempid)
.HasPrecision(18, 0);
}
}
<html>
<head>
<script src="~/Scripts/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function()
{
$.getJSON("http://localhost:51197/api/Test", function (data) {
$.each(data, function (key, val) {
$("<table border='1'><tr><td>" + val.name + "</td><td>" + val.address
+ "</td></tr></table>").appendTo("#tbPerson");
});
});
});
</script>
</head>
<body>
<div id="tbPerson">
</div>
</body>
</html>
$(文档).ready(函数()
{
$.getJSON(“http://localhost:51197/api/Test,函数(数据){
$。每个(数据、函数(键、值){
$(“”+val.name+“”+val.address
+)。附于(“#tbPerson”);
});
});
});
假设您试图使用JavaScript中的API
有两种方法可以消耗相同的能量
$.getJSON()
$.ajax()
有关详细信息,请查看:
JavaScript本质上是非常开放、灵活和面向对象的。
因此,从上面得到的结果将是JavaScript对象,您可以通过循环使用相同的对象来获得预期的结果
function example() {
var data = [{ "Roll": "1", "Name": "A" }, { "Roll": "2", "Name": "B" }, { "Roll": "3", "Name": "C" }];
for (var index = 0; index < data.length; index++) {
var item = data[index];
// Play with your data item for eg
console.log(item.Roll);
console.log(item.Name);
}
// JSON being javascript object
// you can attach properties at run time too
data.Source = "AJAXCall Blah";
// Later in JS same can be used in appropriate scope.
console.log(data.Source);
};
$.each(dataReturnedFromAJAXCall,function(item){
// play with your data here
});
函数示例(){
var data=[{“Roll”:“1”,“Name”:“A”},{“Roll”:“2”,“Name”:“B”},{“Roll”:“3”,“Name”:“C”}];
对于(var index=0;index
假设您试图使用JavaScript中的API
有两种方法可以消耗相同的能量
$.getJSON()
$.ajax()
有关详细信息,请查看:
JavaScript本质上是非常开放、灵活和面向对象的。
因此,从上面得到的结果将是JavaScript对象,您可以通过循环使用相同的对象来获得预期的结果
function example() {
var data = [{ "Roll": "1", "Name": "A" }, { "Roll": "2", "Name": "B" }, { "Roll": "3", "Name": "C" }];
for (var index = 0; index < data.length; index++) {
var item = data[index];
// Play with your data item for eg
console.log(item.Roll);
console.log(item.Name);
}
// JSON being javascript object
// you can attach properties at run time too
data.Source = "AJAXCall Blah";
// Later in JS same can be used in appropriate scope.
console.log(data.Source);
};
$.each(dataReturnedFromAJAXCall,function(item){
// play with your data here
});
函数示例(){
var data=[{“Roll”:“1”,“Name”:“A”},{“Roll”:“2”,“Name”:“B”},{“Roll”:“3”,“Name”:“C”}];
对于(var index=0;index