Guid property regenerating in WebApi POST call

778 views Asked by At

The Guid stored in my entity is regenerating after I make a POST call to my WebApi controller. Here is how it goes, I have the following entity(POCO):

     public class MyEntity : MyBaseEntity
        {
         public virtual int  Prop1{ get; set;}
         private string _ID;
         public virtual string ID { 
          get {
            if(_ID == null) { 
             _ID = Guid.NewGuid().ToString(); 
             return _ID;
            }
            else { 
             return _ID; 
            }
         } 
         set {
          _ID = value;
         } 
       } //End ID property
     } //End Entity

The entity has a property named ID. I do some work on my controller and return a List<MyEntity> to the MVVM front end which is loaded in the Kendo Grid. The List<MyEntity> contains newly generated GUIDs in the ID property. Now user edits a row of the grid which results in an AJAX POST call back to my WebApi controller which has a POST parameter MyEntity :

public void MyMethod(MyEntity myEntity) { ... }

When I check the parameter myEntity, the GUID which is in ID is something different which I had sent from the Client side(front end/ajax call). Say for eg in my ajax call the stringified Json contained "abcd1" in property _ID & ID, but in WebApi's POST method parameter I get a new GUID in ID & _ID lets say "xyz2". Why is this happening? My getter setters are written in a way that if there is a null in _ID then only new GUID will be generated otherwise previous one will be returned.

Also one more question. On Ajax's POST call when the WebApi controller receives the auto-deserialized stringified JSON data as an entity in MyMethod's parameter myEntity, are the getter methods executed for the deserialization or setter methods?

0

There are 0 answers