1

I am using ASP.NET MVC3 and one of my action methods is set up to return a Json response:

[HttpPost]
public JsonResult AddUser(User user)
{
... do something to user and return it ...
return Json(user);

While processing with Entity Framework, the user object gets a few child objects. Json serializer is likely to be unable to cope with the entire object graph, and returns InvalidOperationError error (about a circular reference being identified).

I do not need any of the child objects actually, and I would like to get user serialized without child objects. How I can achieve this?

Thank you in advance!

1 Answer 1

2

You can use the ScriptIgnoreAttribute to tag members you don't want serialized.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I will need those members in other situations? Can I control serialization process from the code?
The only other option with the JavaScriptSerializer used by the JsonResult is to write your own JavaScriptConverter implementation for your type, but you won't be able to use the JsonResult any longer, you would need to write your own result and register your new converter with the serializer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.