Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{ get; set; } used in ViewModel

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?

like image 635
Nate Pet Avatar asked Jul 09 '12 16:07

Nate Pet


4 Answers

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

like image 122
Jf Beaulac Avatar answered Oct 18 '22 19:10

Jf Beaulac


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.

like image 23
Channs Avatar answered Oct 18 '22 18:10

Channs


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.

like image 20
VJAI Avatar answered Oct 18 '22 19:10

VJAI


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;
    }
}
like image 33
Sangeet Shah Avatar answered Oct 18 '22 19:10

Sangeet Shah