将LLBLGen模型类转换为View Model对象?

时间:2014-06-25 03:09:06

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

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

那样的话
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));

    }

1 个答案:

答案 0 :(得分:0)

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

在您的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的替代快捷方式。