我正在尝试通过ajax查询我的帖子表以显示在前端。
这是我的查询:
[HttpGet]
public JsonResult GetGalleryLocations()
{
var data = _context.Posts
.Include(c => c.Category)
.ToList();
return Json(data);
}
然后在我的前端,我得到这样的:
$.ajax({
url: "@Url.Action("GetGalleryLocations", "Home")",
type: "GET",
data: {
title: '',
lat: '',
lng: '',
images: []
},
success: function (data) {
data.forEach(function (element) {
$("#ajaxAppendDIV").append(
// Append HTML here
);
});
}
});
每个帖子都有很多与之相关的图片,所以我需要做这样的查询;
[HttpGet]
public JsonResult GetGalleryLocations()
{
var data = _context.Posts
.Include(c => c.Category)
.Include(c => c.Images)
.ToList();
return Json(data);
}
但是当我执行这个查询时,我会回来:qazxsw poi
这是我两个模型之间的关系:Post model
图像型号:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
public DateTime Created { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
public IEnumerable<Image> Images { get; set; }
// .....
}
当我在查询中使用图像模型执行Include()语句时,我不明白为什么它会破坏?
仅供参考,我将在我的视图中调用以获取帖子的第一张图片:
public class Image
{
public int Id { get; set; }
public string FileName { get; set; }
public Post Post { get; set; }
public int PostId { get; set; }
}
每个循环的查询将与我显示的相同
/ *********更新*********** /
我将Post模型初始化为:
@foreach (var gallery in Model)
{
<img src="@gallery.Images.First().FileName" />
}
这就是我现在试图加载它的方式:public class Post
{
public Post()
{
Images = new List<Image>();
}
public int Id { get; set; }
// .....
public List<Image> Images { get; set; }
}
我弄清楚问题是什么。
我不得不在public void ConfigureServices方法中将它添加到我的Startup.cs类中:
public IActionResult Post(PostViewModel pvm)
//objects to save in our db context
Post post = new Post()
//collection objects we will fill the list in below
Tag tag = new Tag();
Image img = new Image();
//these lists will pass the value of pvm.tags List and pvm.Images List
// To our post object
List<Images> ImgHolderList = new List<Images>();
List<Tags> TagHolderList = new List<Tags>();
//bind properties from vm if pvm is valid
if(!ModelState.IsValid){
return BadRequest("error");
}
//... Know add image and tag objects to to our empty holder Collections
ImgHolderList = pvm.Images;//---------> this is where I think the null was comming ftm
TagHolderList = pvm.Tagsl
//bind properties from vm if pvm is valid
if(ImgHolderList == null || TagHolderList == null){
return NoContent();
}
Post.Images = ImageHolderList;//---------> this is where I think the null was comming ftm
Post.Tags = TagHolderList;//---------> this is where I think the null was comming ftm
//---------better to use some design/mapper but for simplicity bind model here
//...add all other properties
post.primitives = pvm.Id;
post.primitives = pvm.Lat
//.......
// know our object graph is full hopefully
context.Posts.Add(Image1);
context.SaveChanges();
return Ok;
}
know you want to Get this collection very simple
[Produces("application/json")] //with this your api controller returns json with OK()
[Route("api/GetAll")]
public class GetAllController : Controller
{
private readonly ApiContext ctx = ApiContext.db; // OR use DI
// GET api/values
[HttpGet]
public IActionResult Get()
{
var x = ctx.Posts.Include(x => x.Images).Include(x => x.Tags)ToList();
return Ok(x.ToList());
}
我从这个public void ConfigureServices(IServiceCollection services)
{
// Other services here.....
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
}
得到了这个
我稍稍调整了我的模型:
post on stackoverflow
保持我的Image模型一样。
这是我为我的Images数组得到的,这就是我想要的:
public class Post
{
public Post()
{
Images = new List<Image>();
}
// Other attributes here...
public ICollection<Image> Images { get; set; }
}
然后,我只是将图像附加到: