Workarounds for circular references detected in JSON serialized objects in EF

Source: Internet
Author: User

MVC4 EF refers to itself in the data table foreign key, and when converted to JSON, always prompts the error: "Serialization type is ...." A circular reference was detected when the object was ”;

Workaround:

To convert the object to be serialized to an anonymous object, remove the navigation property as follows: It would have been VAR item=db.newscolumns.find (ID), but prompted a circular reference, so rewrite it into the following form var item= Db.newsColumns.Select (x=>new {X.id,x.name,x.parentid}). Where (x=>x.id==ID).

       [HttpGet]        public jsonresult getdatabyid (int  ID)        {             var Item=db.newscolumns.select (x=>new {X.id,x.name,x.parentid}). Where (x=>x.id==ID);             return Json (item, jsonrequestbehavior.allowget);        }

Front desk JS:

$. Get ("/adminnewscolumn/getdatabyid", {id:id}, function (data) {            $ (" #cid "). Val (data[0].id);            $ ("#name"). Val (data[0].name);            $ ("#onename"). Val (data[0].parentid);        })

Resolves a circular reference detected when JSON serializes an object in EF

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.