Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to read Icollection in webapi2/ one to many relationship?

$
0
0

Hi to all . I am very very new to webapi2 so i need a little help.MenuCategory has a public virtual ICollection Items ,and Items have a MenuCategoryID as foreign key, and in mvc view i can read them like this : foreach (var item in Model.Items).What i want is to display items (icollection) that a MenuCategory has , example if MenuCategoryID is coffee , i want to display items is has , like : machiato americano and so on.But i dont know how to read them in postman with webapi2 .Im trying to read child elements in this way :
http://localhost:59381/api/menucategories1/COFFEE

// GET: api/MenuCategories1/5
    [ResponseType(typeof(MenuCategory))]
    public IHttpActionResult GetMenuCategory(string id)
    {


        var x = db.MenuCategories.Where(b => b.MenuCategoryID == id).Select(b => b.Items).ToList();
        if (x == null)
        {
            return NotFound();
        }

        return Ok(x);
    }




Im getting errors:

{     "Message": "An error has occurred.",     "ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",     "ExceptionType": "System.InvalidOperationException",     "StackTrace": null,     "InnerException": {         "Message": "An error has occurred.",         "ExceptionMessage": "Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Item_36D817F86875D1E711ED0FC314B3539B0EEF56E932C694E40A4D8051D4D2D552'. Path '[0][0].MenuCategory.Items'.",         "ExceptionType": "Newtonsoft.Json.JsonSerializationException",         "StackTrace": "
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n
   at Newtonsoft.Json.S



Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>