【问题标题】:Convert LLBLGen model class to View Model objects?将 LLBLGen 模型类转换为 View Model 对象?
【发布时间】:2014-08-15 11:28:23
【问题描述】:

我认为我的标题措辞不正确。我正在尝试返回结果以填充视图。我在浏览器控制台中收到的错误消息是 “在序列化类型对象时检测到循环引用” 所有文档都说要展平对象并排除与模型无关的属性。创建匿名类型似乎对某些人有用。我不能让任何一个工作。如果我尝试类似

var Results = from RS in results
                      select new
                      {
                          BundleId = RS.BundleId
                      };

intellisense 确实可以识别它。有什么建议吗?

控制器

{
public class BundleStatusController : Controller
{


    public ActionResult BundleStatus()
    {
        return View();

    }

    [HttpPost]
    public ActionResult BundleStatusRead([DataSourceRequest] DataSourceRequest request)
    {

            var span = DateTime.Today.AddDays(-1);
            DataAccessAdapter adapter = new DataAccessAdapter();
            EntityCollection allBundles = new EntityCollection(new CarrierBundleEntityFactory());
            RelationPredicateBucket filter = new RelationPredicateBucket(CarrierBundleFields.Date <= span);
            adapter.FetchEntityCollection(allBundles, filter);
            var results = allBundles;

            return Json(results.ToDataSourceResult(request));
        }

}

}

查看

  @{
ViewBag.Title = "BundleStatusGet";
 }

 <div>
@(Html.Kendo().Grid<ZoomAudits.DAL.EntityClasses.CarrierBundleEntity>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.BundleId).Width(140);
        columns.Bound(c => c.CarrierId).Width(190);
        columns.Bound(c => c.Date);
        columns.Bound(c => c.IsSent).Width(110);
    })
    .HtmlAttributes(new { style = "height: 380px;" })
    .Scrollable()
    .Groupable()
    .Sortable()
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
                .Selectable(selectable => selectable
                .Mode(GridSelectionMode.Multiple)
                .Type(GridSelectionType.Cell))
            //.Events(events => events.Change("onChange").Sync("sync_handler")))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("BundleStatusRead", "BundleStatus"))
                //.Update(update => update.Action("EditingInline_Update", "Grid"))
    )
)

更新的控制器

[HttpPost]
    public ActionResult BundleStatusRead([DataSourceRequest] DataSourceRequest request)
    {

        var span = DateTime.Today.AddDays(-1);
        DataAccessAdapter adapter = new DataAccessAdapter();
        EntityCollection allBundles = new EntityCollection(new CarrierBundleEntityFactory());
        RelationPredicateBucket filter = new RelationPredicateBucket(CarrierBundleFields.Date <= span);
        adapter.FetchEntityCollection(allBundles, filter);


        //...Using AutoMapper
        **Mapper**.CreateMap<UtilityWebSite.Models.CarrierBundleModel, ZoomAudits.DAL.EntityClasses.CarrierBundleEntity>();
        List<UtilityWebSite.Models.CarrierBundleModel> viewModelList = Mapper.Map<List<ZoomAudits.DAL.EntityClasses.CarrierBundleEntity>, List<UtilityWebSite.Models.CarrierBundleModel>>(allBundles);
        return Json(viewModelList.ToDataSourceResult(request));

        //...Not using AutoMapper
        List<UtilityWebSite.Models.CarrierBundleModel> viewBundles = new List<UtilityWebSite.Models.CarrierBundleModel>();
        foreach (ZoomAudits.DAL.EntityClasses.CarrierBundleEntity entityBundle in **EntityCollection**)
        {
            UtilityWebSite.Models.CarrierBundleModel model = new UtilityWebSite.Models.CarrierBundleModel();
            model.BundleID = entityBundle.BundleId;
            model.CarrierId = entityBundle.CarrierId;
            viewBundles.Add(model);
        }
        return Json(viewBundles.ToDataSourceResult(request));

    }

【问题讨论】:

    标签: c# asp.net-mvc json kendo-grid llblgenpro


    【解决方案1】:

    您面临需要将数据实体对象与模型对象分开的问题。起初,这似乎比需要的工作多,但随着应用程序的增长,您将看到复制类的好处。以下是我的做法。

    在您的 LLBLGEN 域类中

    namespace ZoomAudits.DAL.EntityClasses
    {
        public class CarrierBundleEntity
        {
           //....Whatever is generated as your Bundle object from the DB
        }
    }
    

    在您的模型 MVC 文件夹中

    namespace MyProjectNamespace.Models
    {
        public class CarrierBundleModel
        {
            [Required]
            [Display(Name = "Bundle ID")]
            public int BundleID{get;set;}
    
            [Required]
            [Display(Name = "Bundle Name")]
            public int BundleName{get;set;}
    
            //...Other properties you will use in the UI That match the LLBLGen Bundle class  
        }
    }
    

    在您的控制器操作中

    [HttpPost]
    public ActionResult BundleStatusRead([DataSourceRequest] DataSourceRequest request)
    {                   
    
            var span = DateTime.Today.AddDays(-1);
            DataAccessAdapter adapter = new DataAccessAdapter();
            EntityCollection allBundles = new EntityCollection(new CarrierBundleEntityFactory());
            RelationPredicateBucket filter = new RelationPredicateBucket(CarrierBundleFields.Date <= span);
            adapter.FetchEntityCollection(allBundles, filter);
    
    
            //...Using AutoMapper
            Mapper.CreateMap<MyProjectNamespace.Models.CarrierBundleModel, ZoomAudits.DAL.EntityClasses.CarrierBundleEntity>();   
            List<MyProjectNamespace.Models.CarrierBundleModel> viewModelList =Mapper.Map<List<ZoomAudits.DAL.EntityClasses.CarrierBundleEntity>, List<MyProjectNamespace.Models.CarrierBundleModel>>(allBundles);
            return Json(viewModelList.ToDataSourceResult(request));
    
            //...Not using AutoMapper
            List<MyProjectNamespace.Models.CarrierBundleModel> viewBundles=new List<MyProjectNamespace.Models.CarrierBundleModel>();
            foreach(ZoomAudits.DAL.EntityClasses.CarrierBundleEntity entityBundle in allBundles)
            {
                MyProjectNamespace.Models.CarrierBundleModel model=new MyProjectNamespace.Models.CarrierBundleModel();
                model.BundleID=entityBundle.BundleId;
                model.CarrierId=entityBundle.CarrierId;
                viewBundles.Add(model);
            }
            return Json(viewBundles.ToDataSourceResult(request));
    
    }
    

    在你看来

    @(Html.Kendo().Grid<MyProject.Models.CarrierBundleModel>()
    

    注意:我使用 AutoMapper,它是一个很棒的映射库,您可以将其添加到您的项目中,如果您的模型和实体数据类型之间存在一对一匹配,则无需编写翻译,并且名字。还有其他可用的映射工具。我只是向您展示了从 DAL 实体转换为模型的替代快捷方式。

    【讨论】:

    • 这就是 LLBLGen 为您生成的 ZoomAudits.DAL.EntityClasses.CarrierBundle 类
    • 只读或者不需要修改。我展示它仅供参考。
    • 当前上下文中不存在名称“Mapper”。在 foreach 循环中,EntityCollection 抛出错误消息“是一种类型,但它像变量一样使用
    • AutoMapper 是一个第三方库,您需要下载并在项目中引用它...使用 AutoMapper;。如果您不想使用它,请删除 AutoMapper 代码。我将 EntityCollection 更改为 allBundles,这是我的示例中的一个错字,抱歉 :)
    • 明白了,我认为这是两个选择。对不起,这真的很新。当我尝试不使用自动映射器时,我无法转换 System.Datime?到 System.DateTime 和我的布尔属性上的相同消息。我有 CarrierId、BundleId、Date、IsSent 作为我的属性
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2016-10-21
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多