2016-09-30 63 views
0

我在我的模型在Visual演播室Asp.net应用程序,我有这个类初始化值型号asp.net C#

public class Goalkeeper 
{  
    public static string Name { get; set; } 
    public static string Position { get; set; } 
    public static int Matches { get; set; } 
    public static int Cleansheets { get; set; }   
} 

有我设置的这些值模型中的一种方式所以我可以在所有不同的视图和控制器动作中使用thme,所以我不需要像这样设置它们(Goalkeeper.Matches = 234;)在我的控制器中的每一个动作中,因为这看起来效率很低。

+0

公众诠释匹配{{返回this.Matches;}集合{this.Matches = 234;}} – Nadeem

回答

1

您可以:

添加一个构造函数模型,在其中设置的初始值:

public class Goalkeeper 
{ 
    public Goalkeeper() { 
     Position = "Goalkeeper"; 
     Matches = 5; 
     Cleansheets = 0; 
    } 

    public static string Name { get; set; } 
    public static string Position { get; set; } 
    public static int Matches { get; set; } 
    public static int Cleansheets { get; set; } 
} 

或者,直接初始化属性:

public class Goalkeeper 
{ 
    public static string Name { get; set; } 
    public static string Position { get; set; } = "Goalkeeper"; 
    public static int Matches { get; set; } = 5; 
    public static int Cleansheets { get; set; } = 0; 
} 
+0

啊很好,谢谢 – Gabc

+0

请标记为答案,如果它帮助; )tnx – Nsevens

+0

现在已经做好了,是第一个定时器 – Gabc

3

根据您正在使用的C#版本可以像这样初始化属性:

public static string Name { get; set; } = "Whatever"; 

在C#这仅适用于6虽然