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

Connect many-to-many objects before POST/PUT'ing

$
0
0

Hi, i have two many-to-many tables, and im wondering how i can POST/PUT a new connection with these two. This is the tables:

    public class Recipe
    {
        public Recipe()
        {
            this.Categories = new ObservableCollection<Category>();
        }

        public int RecipeId { get; set; }
        public string RecipeTitle { get; set; }

        // Foreign keys
        public ObservableCollection<Category> Categories { get; set; }
    }

    public class Category
    {
        public Category()
        {
            this.Recipes = new ObservableCollection<Recipe>();
        }

        public int CategoryId { get; set; }
        public string CategoryName { get; set; }

        // Foreign keys
        public ObservableCollection<Recipe> Recipes { get; set; }
    }

And i have found out how to POST using Postmann, also how to Post a new recipe and a new category, but i cant manage to post a new recipe with a category! This is how a recipe looks like: 

{"RecipeId": 1,"RecipeTitle": "Cheesecake","Categories": [
        {"CategoryId": 1,"CategoryName": "Cakes","Recipes": []
        }
    ],
}



This is the method i use to create a new recipe and category before i send it into the AddRecipeAsync (which does work atleast when i add a recipe without category):

      public void testButtonClick3(object sender, RoutedEventArgs e)
        {
            var newCategory1 = new Category() { CategoryName = "Cakes" };
            var newRecipe1 = new Recipe() { RecipeTitle = "Cheesecake"};

            CookbookDataSource.CookbookDataSource.AddRecipeAsync(newRecipe1);
        }

i have tried to google and check forums here and i really can't figure out how to do this.


Viewing all articles
Browse latest Browse all 4850

Trending Articles



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