【问题标题】:Foreign key not appearing in View外键未出现在视图中
【发布时间】:2021-06-06 19:18:09
【问题描述】:

谁能帮忙,我有一个包含外键(ProductCategory)的模型。我试图调用外键的值,但它们没有出现在视图中,当我在控制器中放置一个断点时字段在原始结果视图中显示为成功,但似乎它们没有被传递到视图。

注意,我正在使用 dev express 生成视图

 public class Product
    {
        public int Id { get; set; }
        public int productCode { get; set; }
        public double price { get; set; }
        public int ProductCategoryId { get; set; }
        public virtual ProductCategory ProductCategory { get; set; }
        public Stock itemNo { get; set; }
    }
  [HttpGet]
        public async Task<IActionResult> Get(DataSourceLoadOptions loadOptions) {
            var product = _context.Product.Select(i => new {
                i.Id,
                i.productCode,
                i.price,
                i.ProductCategoryId,
                i.ProductCategory.categoryDescr      
            });

            return Json(await DataSourceLoader.LoadAsync(product, loadOptions));
        }
@{
    ViewData["Title"] = "Products";
}

<h2 class="content-block">Products</h2>

@(Html.DevExtreme().DataGrid<DevExtremeAspNetCoreApp1.Models.Product>()
    .DataSource(ds => ds.Mvc()
        .Controller("Products")
        .LoadAction("Get")
        .InsertAction("Post")
        .UpdateAction("Put")
        .DeleteAction("Delete")
        .Key("Id")
    )
    .RemoteOperations(true)
    .Columns(columns => {

        columns.AddFor(m => m.productCode);

        columns.AddFor(m => m.price);

        columns.AddFor(m => m.ProductCategoryId);

        columns.AddFor(m => m.ProductCategory.categoryDescr);
    })
    .Editing(e => e.Mode(GridEditMode.Popup)
        .AllowAdding(true)
        .AllowUpdating(true)
        .AllowDeleting(true)
        .Popup(p=>p
               .Title("Product")
               .ShowTitle(true)
               .Width(500)
               .Height(525)
              )
    )

    .Export(e => e.Enabled(true))
)

【问题讨论】:

    标签: asp.net-mvc asp.net-core c#-4.0 devexpress-mvc


    【解决方案1】:

    试试这个:

     var product = _context.Product.Select(i => new {
                    i.Id,
                    i.productCode,
                    i.price,
                    i.ProductCategoryId,
                    ProductCategoryDesc=i.ProductCategory.categoryDescr      
                });
    
    .....
    
     columns.AddFor(m => m.ProductCategoryDesc);
    

    【讨论】:

      【解决方案2】:

      原来我传递的是匿名类型,正确的方法应该是:

      控制器

        public async Task<IActionResult> Get(DataSourceLoadOptions loadOptions) {
                  var product = _context.Product.Select(i => new {
                      i.Id,
                      i.productCode,
                      i.price,
                      i.ProductCategoryId,
                      i.ProductCategory
                  });
      

      查看:

       columns.AddFor(m => m.ProductCategory.Id);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 2018-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多