15

I am using C# along with a ViewModel that passes the view model from the controller to the view.

In my view model, the following seems to work as expected as it passes the Description information from the view back to the controller:

public string Description { get; set; } 

But if I have the following, it won't pass back the Description. Description shows null.

 public string Description  

Why is the { get; set; } important?

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

4 Answers4

22

I dont know much about asp.net MVC / Razor, but there is an important difference between your 2 code samples.

public string Description { get; set; }  

Creates a property, once compiled, there is a generated private field in the class, with get/set methods that access the field. A property declared with {get;set;} is the equivalent of:

    private string _description;
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            this._description = value;
        }
    }

However the following:

public string Description;

Creates a simple public field.

My guess is that razor uses reflection to get values from the ViewModel, and it probably looks for a property, not a field. So it determines that the property does not exist, hence returning null

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
  • This may sound stupid, but is there a way to customize that "value" that is passed to the setter? I was expecting to see set(string value) – Worthy7 Jun 21 '16 at 01:30
  • @Worthy7 I don't understand what you mean by "customize". value is a language contextual keyword when declaring a property, its type is determined by the type of the property. It is a keyword that behaves exactly like a method parameter. For instance you could re-assign it if you want. – Jf Beaulac Jun 21 '16 at 02:52
  • Actually I did figure that out, I also found that the set value type will be the type "string" based on the function description, and this cannot be customized. Interesting. – Worthy7 Jun 21 '16 at 02:56
  • Sweet Mother Of God. I have been wasted couple of hours on this!!! I need to remember this forever!!! Thanks!!!! – TypingPanda Aug 18 '16 at 11:35
10

The below syntax is a C# language feature 'automatic properties'.

public string Description { get; set; }

ASP.NET MVC uses reflection and data-binding which work only with properties and not variables. Using properties for public access is the way to go.

Suggest reading this article where the author retracted his 'dislike' for public properties.

Community
  • 1
  • 1
Channs
  • 2,091
  • 1
  • 15
  • 20
3

The default model binder is the one that binds the request values to properties in models. It binds the values only to public get-set properties and not even to public fields.

If you want to bind the values to fields then you have to write your own model binder but public properties or better than public fields so you don't need that.

VJAI
  • 32,167
  • 23
  • 102
  • 164
1

It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):

private string name;
public string Name
{
    get
    {
        return this.name;
    }
    set
    {
        this.name = value;
    }
}
Sangeet Shah
  • 3,079
  • 2
  • 22
  • 25