I have a situation where my HTML form represents a different data structure than the existing structure on the server.
To exemplify my problem, I will present a hypothetical situation, to present which solution I need:
My class:
public class Pessoa{
public long Codigo {get;set;}
public Estado Estado {get;set;}
}
[ModelBinder(typeof(EstadoBinder))]
public class Estado{
public static Estado ObtenhaEstado(long codigo){
// Obtêm instância de estado a partir do código
}
public long Codigo {get;set;}
}
My HTML:
<div id='formularioPessoa'>
<input type='text' name='Codigo' />
<input type='text' name='Estado' />
</div>
As you can see in the HTML section, I need the 'State' input to reflect the State object of my Person class.
For the interaction of CLIENT to SERVER, I have already achieved a solution by implementing a custom binder, implementing the IModelBinder interface. The solution was excellent, as it did not require any class other than the state itself to know this particularity.
My need:
Now, I need something to handle the interaction of SERVER to CLIENT, when there is a serialization of a C # object to a JSON object.
{
"Codigo": "1",
"Estado": "2"
}
Instead of:
{
"Codigo": "1",
"Estado": {
Codigo: "2"
}
}
I have already researched a lot of English material, including looking for something associated with DateTime, in relation to 'Custom serializers', 'Override Json method of controller', however, I did not find any elegant solution as the solution I found for Client & gt ; Server (IModelBinder).
One caveat:
- I can not use Html Helpers, nor other ASP.NET MVC code in my CSHTML file.