Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NETMVC--使用类作为模型?_C#_Asp.net Mvc_Class_Asp.net Mvc Viewmodel - Fatal编程技术网

C# .NETMVC--使用类作为模型?

C# .NETMVC--使用类作为模型?,c#,asp.net-mvc,class,asp.net-mvc-viewmodel,C#,Asp.net Mvc,Class,Asp.net Mvc Viewmodel,在我的MVC应用程序中,几个视图模型将非常相似。与其每次都复制模型,我想我可以只创建一个类。我不确定的是如何在每个模型中包含该类 例如,假设我的一个模型如下所示: public class AccountProfileViewModel { public string FirstName { get; set; } public string Lastname { get; set; } public AccountProfileViewModel() { } } 但我

在我的MVC应用程序中,几个视图模型将非常相似。与其每次都复制模型,我想我可以只创建一个类。我不确定的是如何在每个模型中包含该类

例如,假设我的一个模型如下所示:

public class AccountProfileViewModel
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public AccountProfileViewModel() { }
}
但我知道FirstName和LastName将在许多模型中广泛使用。因此,我创建了一个包含AccountProfile的类库:

namespace foobar.classes
{
    public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
}

回到模型中,我将如何包含类,以便FirstName和LastName在模型中,但不是专门创建的

创建一个基类,然后使用继承,您可以访问这些公共属性

public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }

public class OtherClass : AccountProfile 
    {
        //here you have access to FirstName and Lastname by inheritance
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }

除了使用继承,您还可以使用组合来实现相同的目标

可能是这样的:

public class AccountProfile
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
}

public class AccountProfileViewModel
{
    // Creates a new instance for convenience
    public AnotherViewModel() { Profile = new AccountProfile(); }

    public AccountProfile Profile { get; set; }
}

public class AnotherViewModel
{
    public AccountProfile Profile { get; set; }

    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

您还可以实现像
IProfileInfo
这样的接口,这可能更可取,因为类可以实现多个接口,但只能从一个类继承。将来,您可能希望在代码中添加一些其他统一的方面,要求继承它,但您可能不一定希望某些类继承自具有Firstname和Lastname属性的基类。如果您使用的是VisualStudio,它将自动为您实现接口,因此不会涉及任何实际的额外工作

public class AccountProfile : IProfileInfo
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
}

public interface IProfileInfo 
{
    string Firstname {get;set;}
    string Lastname {get;set;}
}

这段时间太长,无法插入评论,因此这只是基于您已经收到的答案的评论

只有当创建的新类基本上是相同类型的对象,只是略有不同时,才应该使用继承。如果试图将两个单独的类关联在一起,则应使用property方法。遗产大概是这样的

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
}

public class Teacher : Person 
{
    public string RoomNumber { get; set; }
    public DateTime HireDate { get; set; }
}

public class Student : Person
{
    public string HomeRoomNumber { get; set; }
    public string LockerNumber { get; set; }
}
构图应该这样使用

public class Address 
{
    public string Address1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

public class StudentViewModel
{
    public StudentViewModel ()
    {
        Student = new Student();
        Address = new Address();
    }
    public Student Student { get; set; }
    public Address Address { get; set; }

}

不知道为什么我没有想到继承。太好了!好,好。这更是我的想法。我来读你链接的帖子。问题:在上面的第三个例子中,我将如何访问FirstName?Profile.FirstName对我不起作用。添加构造函数来初始化属性可能会为您节省一些麻烦
public AnotherViewModel(){Profile=new AccountProfile();}
@CaseyCrookston可能是JamieD77指出的情况,配置文件引用可能是空的?哇,好的。很多很棒的答案。还有很多东西要学。我要学习更多关于类和接口之间的区别。非常感谢。这是对接口与类问题的一个很好的回答。谢谢JamieD77。这很有道理。