【问题标题】:How do I create a model based on another model?如何基于另一个模型创建模型?
【发布时间】:2021-04-24 11:27:54
【问题描述】:

Cum creez un model de bazat pe un alt model? Pentru că aş dori să selectez doar proprietățile necesare.

例子:

public class Test {
    public string prop1 { get; set; }
    public string prop2{ get; set; }
    public decimal prop3 { get; set; }
}

我想要的样子:

public class TestViewModel {
    public string prop1 { get; set; }
    public decimal prop3 { get; set; }
}

我想这样做,因为我想制作视图,并且模型只包含必要的数据

【问题讨论】:

  • 你可以使用 AutoMapper

标签: c# model


【解决方案1】:

恕我直言,您应该从头开始创建视图,没有任何继承。

Model和View之间唯一的关系应该是View的c-tor,它会得到一个Model类型的参数。

【讨论】:

    【解决方案2】:

    试试这个:

    public class TestViewModel
    {
        public string prop1 { get; set; }
        public decimal prop3 { get; set; }
    }
    
    public class Test: TestViewModel
    {
          public string prop2{ get; set; }
       
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 2011-07-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2016-01-14
      • 1970-01-01
      相关资源
      最近更新 更多