Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey i have a implementation i can to figure out to do in asp.net 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:

C#
public class Recipe
    {
        public int RecipeID { get; set; }
        [MaxLength(255)]
        public string Title { get; set; }
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }
        [DataType(DataType.MultilineText)]
        public string Story { get; set; }
        public string ImageUrl { get; set; }
        public virtual ICollection<RecipeIngredient> Ingredients { get; set; }
    }


C#
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:
C#
¨
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.

HTML
Add ingredients: 
        <input id="txtAmount" type="text" />
        <select id="Select1">
            <option>DL</option>

        </select>
<input type="text" name="completeMe" id="completeMe" />
        <input type="submit" value="Search" />
        </p>
<script type="text/javascript">

$(function() {

$("#completeMe").autocomplete({
    autoFocus: true,
    source: '/Recipe/AutoCompleteIngredients',
    minLength: 1,
    select: function(event, ui) {
    }
});
});

</script>


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.
/Anders
Posted

1 solution

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 ?
 
Share this answer
 
Comments
AndersLind 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900