In my class i’ve got:
[DataMember(Name = "jsonMemberName", EmitDefaultValue = false,
IsRequired = false)]
public List<string> Member { get; set; }
After passing the object through controller’s Json(obj) that retruns System.Web.Mvc.JsonResult: i’ve got serialized json: {Member:…} but not {jsonMemberName:…}, so it doesn’t look at DataMember(Name = “jsonMemberName”).
If I use serialization from System.Runtime.Serialization.Json everithing’s works fine as expected.
What can be wrong?
The JsonResult action which you are returning from the controller action (using
return Json(...)) internally relies on the JavaScriptSerializer class. This class doesn’t take into account anyDataMemberattributes on your model.You could write a custom ActionResult which uses the serializer in the
System.Runtime.Serialization.Jsonnamespace.For example:
and then in your controller action: