2016-09-30 32 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雖然