MVC 2 – Model binding happens automatically for properties not fields

For some reason, when developing MVC 2 (C#) projects, post data are not automatically bound to class members defined as Fields, but they will be bound to Properties.

So this won’t work:

public string Name;

but something like this WILL work:

private string _name;
public string Name
  get { return _name; }
  set { _name = value; }

// I suppose this will work as well

public string Name { get; set; } 

Hope this helps someone.

This entry was posted in C#. Bookmark the permalink.