ASP.NET MVC UpdateModel - fields vs properties??

Posted by mrjoltcola on Stack Overflow See other posts from Stack Overflow or by mrjoltcola
Published on 2010-04-15T18:18:05Z Indexed on 2010/04/15 20:43 UTC
Read the original article Hit count: 282

Filed under:

I refactored some common properties into a base class and immediately my model updates started failing. UpdateModel() and TryUpdateModel() did not seem to update inherited public properties.

I cannot find detailed info on MSDN nor Google as to the rules or semantics of these methods. The docs are terse (http://msdn.microsoft.com/en-us/library/dd470933.aspx), simply stating:

Updates the specified model instance using values from the controller's current value provider.

SOLVED: MVC.NET does indeed handle inherited properties just fine. This turned out to have nothing to do with inheritance. My base class was implemented with public fields, not properties. Switching them to formal properties (adding {get; set; }) was all I needed. This has bitten me before, I keep wanting to use simple, public fields. I would argue that fields and properties are syntactically identical, and could be argued to be semantically equivalent, for the user of the class.

© Stack Overflow or respective owner

Related posts about asp.net-mvc