Click here to Skip to main content
13,141,068 members (50,000 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hey i have a implementation i can to figure out to do in MVC3 web application.

I have to Entities with a many to many relation. I have already existing controller and views for both classes, but my problems is i can't figure out how to implement the crud relation between the two entities.

My two classes are as following:

public class Recipe
        public int RecipeID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Story { get; set; }
        public string ImageUrl { get; set; }
        public virtual ICollection<RecipeIngredient> Ingredients { get; set; }

public class Ingredient
        public int IngredientID { get; set; }
        public string Name { get; set; }
        public virtual IngredientCategory Category { get; set; }

Theses are joinet by an entity holding some extra information like this:
public class RecipeIngredient
        public int RecipeIngredientID { get; set; }
        public int IngredientID { get; set; }
        public int RecipeID { get; set; }
        public string Amount { get; set; }
        public string Measurement { get; set; }
        public virtual Recipe Recipe { get; set; }
        public virtual Ingredient Ingredient { get; set; }

Soo far so good. Then i have added this to the recipe edit view.

Add ingredients: 
        <input id="txtAmount" type="text" />
        <select id="Select1">
<input type="text" name="completeMe" id="completeMe" />
        <input type="submit" value="Search" />
<script type="text/javascript">
$(function() {
    autoFocus: true,
    source: '/Recipe/AutoCompleteIngredients',
    minLength: 1,
    select: function(event, ui) {

I want to be able to add ingredients by searching for ingredient and typing amount and measurement in the input fields. Searching works fine with jquery autocomplete, but i can't wrap my head around how i takes the selected ingredient from the jquery autocomplete and creates an RecipeIngredient.

I know a other solution is the checkbox approach, but this is not what i want. I can't seem to find any examples on this, and don't even know if i'm doing the right approach or a entire other way would be better.

Any help is greatly appreciated.
Posted 8-Jan-13 4:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It looks to me like the autocomplete will give you the name and id of an ingredient. Then you will type in the rest of the information( quantity, etc ) and then store those values in the DB ( probably in a call that checks first if that ingredient is already in the recipe and, if so, updates it instead.

Your joining class has the ids of both objects, why does it contain instances of the objects as well ? This is not EF, right, it's just classes you're writing ?
dm52j 30-Jan-13 10:04am
Hey i fairly new to EF and .net 4.0. The classes are written by me by as EF code first. I thought i had to have the ID of the objects in the joining class. But i guess they can be removed if EF have no use for them, cause i don't use them.

And about your suggestion your are absolutely right. This is what i want. But when i click the button to add the ingredient, i catch the HttpPost Edit event in the controller. But how does i go about getting the Ingredient object found by the AutoComplete search and the amount typed in the textfield and the measurement in the select??

Any help with this??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100