Syntax for declaring an initialized dictionary Model Property in ASP.NET MVC?

jerryh91

I'm attempting to add a dictionary property in one of my model classes that has a set list of key value pairs. However, I don't know how to declare this with the {get; set;} syntax that signals this is a model property and not a simple field.

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", ""},

        }
    }

}

Satpal

Declare a property and the you can use constructor to initialize it.

public class Profile
{ 

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

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

        };
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC Scaffolding View - Missing Model Property

ASP NET MVC C# - .Model is a property but it used like a type

ASP.NET Core MVC List property of model empty in view

How to fix "This dictionary requires a model item of type" in Asp.NET MVC?

form for a model that contains a list property in asp.net core 3 MVC

ASP.NET MVC4 Model Binding Issue with Nullable DateTime property

ASP.NET MVC 5 Html.HiddenFor renders incorrect property value but Model.Property renders correct value

ASP.NET MVC 5.0 model class - how to have a property in model class but not create a Database field

tips for ASP.net MVC And Data Model

Asp.Net MVC Model Binding to a property type System.Uri got issues with Mono

Unique key applying on asp.net MVC 5 model property

Can an ASP.NET MVC model class have a property called Model?

Custom Regular expression in model property ASP.NET MVC4

Asp.net- Mvc Complex Model Binding

Implicitly declaring a parameter in an ASP.NET MVC action

Dictionary<,> Binding to a Controller in Asp.Net MVC

How to show YouTube link via model property using asp.net MVC?

ASP.NET MVC 5 The model item passed into the dictionary is of type 'Name.Models.IndexViewModel', but this dictionary requires a model item of type

ASP.NET MVC ViewModel Property is null

Storing a MVC Model Property to a JavaScript var object syntax error

Model error in ASP.NET MVC 5

How bind property in model from json in asp.net mvc5

Asp.net MVC model won't bind list property to view

ASP.Net MVC: How to add validation to any model property on the fly

How to handle a unit conversion property in ASP.Net Core MVC model?

Model binding collection of Objects as one property of Model in ASP.Net Core MVC

How to reference a view model property in Asp.Net MVC - Razor view

ASP.NET Core MVC ConcurrencyCheck is failing when the model property has the [ConcurrencyCheck] attribute

ASP.NET MVC error: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1