{ get; set; } used in ViewModel

2019-01-26 05:53发布

问题:

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; }

so important?

回答1:

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



回答2:

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.



回答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.



回答4:

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;
    }
}