模型列表到控制器中的ViewModel列表

时间:2016-02-13 20:43:46

标签: c# asp.net-mvc entity-framework

我有一个DBContext“StatusDBContext”,一个带有一些(n)属性的模型“Status_T”和一个具有少量Model属性的相应ViewModel“Status”。我首先使用Entity Framework和数据库中的代码。

的DbContext:

namespace xxx.Areas.SysParam.Models
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class StatusDbContext : DbContext
    {
        public StatusDbContext()
            : base("name=xxxConnectionString")
        {
        }

        public virtual DbSet<STATUS_T> STATUS_T { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<STATUS_T>().ToTable("STATUS_T");
        }
    }
}

型号:

namespace xxx.Areas.SysParam.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("STATUS_T")]
    public partial class STATUS_T
    {
        [Key]
        [Column(Order = 0)]
        [StringLength(35)]
        public string TYPE { get; set; }

        [Key]
        [Column(Order = 1)]
        public byte STATUS { get; set; }

        [StringLength(30)]
        public string DESCRIPTION { get; set; }

        [Key]
        [Column(Order = 2)]
        [StringLength(2)]
        public string LANG_CODE { get; set; }

        public DateTime UPD_DTIME { get; set; }

        public DateTime? DELETE_DTIME { get; set; }

        public short? VER_NO { get; set; }

        [StringLength(1)]
        public string STAT_USE { get; set; }
    }
}

查看型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using xxx.Areas.SysParam.Models;

namespace xxx.Areas.SysParam.ViewModels
{
    public class Status
    {
        [Display(Name = "Status ID")]
        public byte STATUS { get; set; }

        [Display(Name = "Description")]
        public string DESCRIPTION { get; set; }

        [Display(Name = "Type")]
        public string TYPE { get; set; }

        [Display(Name = "In Use")]
        public string STAT_USE { get; set; }
    }
}

在介绍ViewModel之前,Controller看起来像:

public ActionResult StatusView()
{
    List<Models.STATUS_T> StatusList = new List<Models.STATUS_T>();

    using (var status = new Models.StatusDbContext())
    {
        StatusList = Status.STATUS_T.ToList();
    }
    return View(StatusList);
}

现在我有两个问题。

1)当我搜索Model,ViewModel和DataAnnotations时,有点明确的是应该将DataAnnotations添加到ViewModel,但是在现有数据库的Code First中,使用很少的DataAnnotations生成模型,因此这里最好的做法是复制属性从Model到ViewModel并在需要时添加更多DataAnnotations?

2)如何在Controller代码中用ViewModel替换Model?

帮助将不胜感激。谢谢!!!

更新:控制器:

public ActionResult DWPStatusView()
{
    var DWPStatusList = new List<ViewModels.DWPStatus>();

    using (var DWPStatus = new Models.DWPStatusDbContext())
    {
        DWPStatusList = DWPStatus.DWP_STATUS_T.ToList().Select(p => new ViewModels.DWPStatus(p)).ToList();
    }
    return View(DWPStatusList);
}

1 个答案:

答案 0 :(得分:3)

1.是的,最佳做法是创建单独的视图模型类并从DB模型中复制必要的属性,因为:

  • 可能发生DB模型具有不需要的视图属性
  • 视图模型可能需要其他属性并将它们添加到数据库模型中 是不好的做法

View模型通常具有验证数据注释属性,因此最好将它们与DB注释分开。

2

public ActionResult StatusView()
{
    var statusList = new List<ViewModels.Status>();

    using (var status = new Models.StatusDbContext())
    {
        statusList = Status.STATUS_T.ToList().Select(p => new ViewModels.Status {
            Property1 = p.Property1,
            Property2 = p.Property2,
            ...
        }).ToList();
    }
    return View(statusList);
}

您可以将属性映射放入视图模型构造函数中,然后选择数据:

statusList = Status.STATUS_T.ToList().Select(p => new ViewModels.Status(p)).ToList();

更新并查看模型:

public class Status
{
    public Status(){}
    public Status(Models.STATUS_T model)
    {
        //here comes mapping
    }
    ....
}