将LLBLGen模型类转换为View Model对象? [英] Convert LLBLGen model class to View Model objects?

查看:114
本文介绍了将LLBLGen模型类转换为View Model对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题的措词不正确.我试图返回结果以填充视图.我在浏览器控制台中收到的错误消息是 序列化类型的对象时检测到循环引用" 所有文档都说要展平对象并排除与模型无关的属性.创建一个匿名类型是一种变通方法,似乎可以解决某些问题.我都不能上班.如果我尝试类似的东西

I do not think I worded the title correctly. I am trying to return results to populate a view. the error message I get in the browser console is "A circular reference was detected while serializing an object of type" all of the documentation says to flatten out the object and exclude properties that are not related to the model. creating a anonymous type is a work around that seems to work for some. I cannot get either to work. if I try something like

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

intellisense确实将其拾起.有什么建议吗?

intellisense does pick it up. Any suggestions?

控制器

{
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));

    }

推荐答案

您正面临一个问题,即您的数据实体对象需要与模型对象分开.最初,这似乎比需要的工作还要多,但是随着应用程序的增长,您将看到复制类的好处.这就是我的解决方法.

You are facing an issue where your Data Entity object needs to be separated from your Model objects. At first this seems like more work than needed, but as your application grows you will see the benefit in duplicating you classes. Here is how I would go about it.

在您的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实体转换为Model的另一种快捷方式.

NOTE: I use AutoMapper which is a great mapping library that you can add to your project and never have to write translations if there is a one to one match between your model and entity data types and names. There are other mapping tools available. I just included to show you an alternative shortcut to translating from DAL entity to Model.

这篇关于将LLBLGen模型类转换为View Model对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆