【问题标题】:complex object is not passed entirely in api call (Angular + net core 3)复杂对象未完全在 api 调用中传递(Angular + net core 3)
【发布时间】:2020-07-16 18:59:56
【问题描述】:

我正在尝试向 Angular 客户端发送一个复杂对象。对象的类如下:

public class ProductListModel
{
    public int TotPages { get; set; } = 1;
    public int CurPage { get; set; } = 1;
    public long CatId { get; set; } = -1;

    public string Search { get; set; } = "";
    public string Title = "";
    public Category[] Categories;
    public Product[] Products;
    public bool HasCarousel { get; set; }
    public bool Discounted { get; set; } = false;
}

我在 Angular 中定义了一个接口,就像类一样:

 interface ProductListModel {
  totPages: number;
  curPage: number;
  catId: number;
  search: string;
  title: string;
  hasCarousel: boolean;
  discounted: boolean;
  categories: Category[];
  products: Product[];
}

然后我的 api 控制器中有:

 [HttpGet]
     [Route(nameof(GetProducts))]
     public ProductListModel GetProducts(int curPage = 1, long catId= -1, bool discounted = false, string search="")
     {
        // uSession.LastUserUrl = Request.PathBase + Request.Path + Request.QueryString;
        var plm = new ProductListModel();
        int prodPerPage = 10;

        IQueryable<Product> qp;
        if (search != "")
        {
            plm.Title = "Found Products";
            qp = dbContext.Products.Where(p => p.Active && p.Name.Contains(search)).OrderBy(p => p.Name);
        }
        else
        if(discounted)
        {
            plm.Title = "Discounted Products";
            qp = dbContext.Products.Where(p => p.Active && p.DiscountPerc>0).OrderBy(p => p.Name);
        }
        else
        {
            long catIdNone = dbContext.Categories.Where(c => c.Name == "_None").FirstOrDefault().Id;
            if (catId == -1 || catId == catIdNone)
            {
                plm.Title = "All Products";
                qp = dbContext.Products.Where(p => p.Active).OrderBy(p => p.Name);
            }
            else
            {
                var catName = dbContext.Categories.Where(c => c.Id == catId).FirstOrDefault().Name;
                plm.Title = "Category: " + catName;
                qp = dbContext.Products.Where(p => p.Active).Where(p => p.CategoryId == catId).OrderBy(p => p.Name);
            }
        }

        int totPages = (int)Math.Ceiling((decimal)qp.Count() / prodPerPage);

        if (qp!=null)
        {
            plm.Products = qp.Skip((curPage - 1) * prodPerPage).Take(prodPerPage).ToArray();
        }
        var catl = dbContext.Categories.OrderBy(c => c.Name).ToArray();
        if (catl!=null)
        {
            plm.Categories = catl;
        }
        plm.CatId = catId;
        plm.CurPage = curPage;
        plm.TotPages = totPages;
        plm.HasCarousel = (catId == -1) && (!discounted);
        plm.Discounted = discounted;
        plm.Search = search;

        return plm;
     }

最后是角度调用:

http.get<ProductListModel>(baseUrl + 'api/ajax/GetProducts').subscribe(result => {
  this.ProductListModel = result;
  console.log(result);
}, error => console.error(error));

但我得到的只是非数组属性,数组产品和类别甚至不存在于结果中。在 Postman 中,我得到相同的结果,请参见屏幕截图。为什么会发生这种情况以及您建议的解决方案。谢谢

【问题讨论】:

    标签: angular asp.net-core


    【解决方案1】:

    我发现缺少的属性没有getter 和setter。添加这个解决了这个问题。

    【讨论】:

    • 试试什么?我一直试图理解这个问题一个小时,然后我寻求帮助。当我再次阅读时,我的问题帮助我找到了答案。
    • 我不会删除这个问题,因为它可能对其他人有帮助
    • 是的,我看到没有 getter 和 setter,这让我明白了解决方案。我试过了,效果很好。
    • 你只是在争论,因为我自己找到了解决方案。
    猜你喜欢
    • 2021-02-22
    • 2021-11-28
    • 2021-02-07
    • 2021-03-13
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多