C# 在ASP.NET MVC中声明初始化字典模型属性的语法?

C# 在ASP.NET MVC中声明初始化字典模型属性的语法?,c#,asp.net,asp.net-mvc,dictionary,C#,Asp.net,Asp.net Mvc,Dictionary,我试图在我的一个模型类中添加一个dictionary属性,该模型类具有一组键值对列表。然而,我不知道如何用{get;set;}语法声明它,它表示这是一个模型属性,而不是一个简单的字段 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ContosoUniversity.Models { public class Profile {

我试图在我的一个模型类中添加一个dictionary属性,该模型类具有一组键值对列表。然而,我不知道如何用{get;set;}语法声明它,它表示这是一个模型属性,而不是一个简单的字段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContosoUniversity.Models
{
    public class Profile
    { 
    //classNameID or ID is interpreted by EF as PK.
    public int ID { get; set; }
    public string UserName { get; set; }
    public string Age { get; set; }
    public string Location { get; set; }
    public string Gender { get; set; }

  //How to declare this property with get, set and initialized key/val pairs?
    public string Dictionary<string, string> ProfileDetails =
        new Dictionary<string, string>()
        {
            {"HighSchool", ""},
            {"UndergraduateSchool", ""},
            {"GraduateSchool", ""},

        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间:大学。模型
{
公共班级简介
{ 
//classNameID或ID被EF解释为PK。
公共int ID{get;set;}
公共字符串用户名{get;set;}
公共字符串年龄{get;set;}
公共字符串位置{get;set;}
公共字符串{get;set;}
//如何使用get、set和initialized key/val对声明此属性?
公共字符串字典配置文件详细信息=
新字典()
{
{“高中”,
{“本科学校”,
{“研究生院”,
}
}

}

声明一个属性,您可以使用构造函数对其进行初始化

public class Profile
{ 

    public Dictionary<string, string> ProfileDetails {get; set;}

    public Profile()
    {
        ProfileDetails = new Dictionary<string, string>()
        {
            {"HighSchool", ""},
            {"UndergraduateSchool", ""},
            {"GraduateSchool", ""},

        };
    }
}
公共类配置文件
{ 
公共字典配置文件详细信息{get;set;}
公众简介()
{
ProfileDetails=新字典()
{
{“高中”,
{“本科学校”,
{“研究生院”,
};
}
}

声明一个属性,您可以使用构造函数对其进行初始化

public class Profile
{ 

    public Dictionary<string, string> ProfileDetails {get; set;}

    public Profile()
    {
        ProfileDetails = new Dictionary<string, string>()
        {
            {"HighSchool", ""},
            {"UndergraduateSchool", ""},
            {"GraduateSchool", ""},

        };
    }
}
公共类配置文件
{ 
公共字典配置文件详细信息{get;set;}
公众简介()
{
ProfileDetails=新字典()
{
{“高中”,
{“本科学校”,
{“研究生院”,
};
}
}
声明:

class Profile{
  private Dictionary<string,string> _profileDetails;
  public Dictionary<string,string> ProfileDetails { get { return _profileDetails; } }
  public Profile() { _profileDetails = new Dictionary<string,string>(); }
}
声明:

class Profile{
  private Dictionary<string,string> _profileDetails;
  public Dictionary<string,string> ProfileDetails { get { return _profileDetails; } }
  public Profile() { _profileDetails = new Dictionary<string,string>(); }
}
公共字典ProfileDetails{get;set};
//自动属性语法。此属性将由相同类型的字段(即Dictionary)自动支持

对于初始化,使用类构造函数在其中添加keyValuePairs

public Profile()
{
    ProfileDetails = new Dictionary<string, string>(){
        {"key01", "value01"},
        {"key02", "value02"},
        {"key03", "value03"}
    };  //This syntax is called collection initializer.
}
public Profile()
{
ProfileDetails=新字典(){
{“key01”,“value01”},
{“key02”,“value02”},
{“key03”,“value03”}
};//此语法称为集合初始值设定项。
}
这样,您就可以在这里实现您的目标。

公共字典ProfileDetails{get;set};
//自动属性语法。此属性将由相同类型的字段(即Dictionary)自动支持

对于初始化,使用类构造函数在其中添加keyValuePairs

public Profile()
{
    ProfileDetails = new Dictionary<string, string>(){
        {"key01", "value01"},
        {"key02", "value02"},
        {"key03", "value03"}
    };  //This syntax is called collection initializer.
}
public Profile()
{
ProfileDetails=新字典(){
{“key01”,“value01”},
{“key02”,“value02”},
{“key03”,“value03”}
};//此语法称为集合初始值设定项。
}

这样你就可以在这里实现你的目标。

哦,非常感谢你,忘记了一切都是“类”,甚至是模型类!哦,太感谢你了,忘了一切都是“类”甚至模型类!