创建两个EF模型在ASP.Net MVC5一个ViewModel [英] Creating a ViewModel from two EF Models in ASP.Net MVC5

查看:225
本文介绍了创建两个EF模型在ASP.Net MVC5一个ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找周围,真的找不到关于如何建立一个视图模型,然后填补从我的EF模型中的数据一个像样的答案。这两种型号EF我要推到一个单一的视图模型是:

I have been searching around and really can not find a decent answer on how to build a ViewModel and then fill that with the data from my EF model. The two EF models I want to push into a single ViewModel are:

public class Section
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), HiddenInput]
    public Int16 ID { get; set; }

    [HiddenInput]
    public Int64? LogoFileID { get; set; }

    [Required, MaxLength(250), Column(TypeName = "varchar"), DisplayName("Route Name")]
    public string RouteName { get; set; }

    [Required, MaxLength(15), Column(TypeName = "varchar")]
    public string Type { get; set; }

    [Required]
    public string Title { get; set; }

    [HiddenInput]
    public string Synopsis { get; set; }

    [ForeignKey("LogoFileID")]
    public virtual File Logo { get; set; }
}

public class File
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 ID { get; set; }

    [Required, MaxLength(60), Column(TypeName = "varchar")]
    public string FileName { get; set; }

    [Required, MaxLength(50), Column(TypeName = "varchar")]
    public string ContentType { get; set; }
}

和我想建立一个视图模型,看起来像:

And I would like to build a ViewModel that looks like:

public class SectionViewMode
{
    public Int16 SectionID { get; set; }

    public bool HasLogo { get; set; } //Set to True if there is a FileID found for the section
    public string Type { get; set; }
    public string Title { get; set; }
    public string Synopsis { get; set; }
}

我会假设这将是最好创建的视图模型,所以当被称为上的数据填充的构造方法,但似乎我无法找到或者弄清楚是我如何去填补这一数据。

I would assume it would be best to create a constructor method in the ViewModel so when NEW is called on it the data is filled but what I can not seem to find or figure out is how I go about filling that data.

推荐答案

这是一个紧耦合作为您的视图模型连接到您的域模型法。我个人并不preFER这种方式。我会去从我的域模型映射到另一个视图模型映射方法

This is a tightly coupled approach as your view models are coupled to your domain models. I personally do not prefer this way. I would go for another mapping method which maps from my domain model to viewmodel

如果你真的想要的构造方法,你可以通过部分对象的构造和设置的属性值。

If you really want the constructor approach, You may pass the Section object to the constructor and set the property values.

public class SectionViewModel
{
    public SectionViewModel(){}
    public SectionViewModel(Section section)
    {
       //set the property values now.
       Title=section.Title;
       HasLogo=(section.Logo!=null && (section.Logo.ID>0)); 
    }

    public Int16 SectionID { get; set; }    
    public bool HasLogo { get; set; } 
    public string Type { get; set; }
    public string Title { get; set; }
    public string Synopsis { get; set; }
}

当你想创建视图模型对象,

and when you want to create your view model object,

Section section=repositary.GetSection(someId);
SecionViewModel vm=new SectionViewModel(section);

这篇关于创建两个EF模型在ASP.Net MVC5一个ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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