Siddharth692
Siddharth692

Reputation: 73

MVC Core 2.0 "Post" action returns null view model

I am working with ASP .Net core 2.0 MVC website. In this, I have one controlller, one view model and a view.

View Model is of type

 class ViewModelClassName
{
    public int property1;
    public Dictionary<string, Dictionary<string, string>> property2;
}

I come to know through previous thread that if properties are not displayed in the editable fields, they will return null. In view I displayed all the properties either in hidden or visible form in the fields. Still form submission returns null. Is Dictionary as shown in code a valid way to store? If not, what can be the alternatives?

Upvotes: 1

Views: 87

Answers (1)

Syed Abdul Aala
Syed Abdul Aala

Reputation: 167

Use properties instead of field. It should be like

class ViewModelClassName
{
    public int Property1 { get; set; }
    public Dictionary<string, Dictionary<string, string>> Property2 { get; set; }
}

Reason behind using properites is because default model binder works with properpties.

Update: If you need to understand how dictionary binding work, refer to below link http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Upvotes: 2

Related Questions