MVC3 Json function hides certain properties

Is there a way in MVC3 to set the properties that the Json function outputs?

i.e., the properties of my model have an attribute that tells Json functions not to output them.

+3
source share
2 answers

It looks like ScriptIgnoreAttribute will do what you want. Just decorate any property that you do not want to serialize with it.

+6
source

Use an anonymous method to do this:

therefore instead

return Json(it);

do

return Json(new {
  it.Name,
  CreatedAt = it.CreatedAt.ToString("D")
  // And so on...
});

this way you explicitly publish (map) a set of attributes on the Internet that guarantees access only to the allowed properties from JSON.

, JSON.NET, . ( , HideAttribute ). JSON.NET Controller.Json (SmartJson ). , .

+1

All Articles