Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery MVC3 Entities
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:
 
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; }
    }
 
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">
            <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 8-Jan-13 5:38am

1 solution

Rate this: bad
good
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 ?
  Permalink  
Comments
dm52j at 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
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
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