Model binding Enum back to Object

Whe passing data from client (ko data back to MVC) where the object is an Enum of say 2 values. We need to either setup a Model Binder for that or take a shortcut ​as follows:

<checkbox data-bind=”checked: viewModel.myStatus”>

Enum MyStatus {Complete=1, NotStarted=0}

And if myStatus is of type MyStatus then, we can say in ajax before postback:

postbackObject.myStatus = (viewModel.myStatus ==0) ? “NotStarted” : “Complete”. And the MVC will figure out the Enum string to translate it back to the correct enum and we will get a valid Model in [FromBody].

Thats all

Leave a Reply

Your email address will not be published. Required fields are marked *