1

I am trying to make an application which stores "Ingredients" for a recipe in the database. One recipe can have multiple ingredients which will be entered from new text boxes for each new ingredient.

I am confused of how to configure this in my controller on post to save in the database.

e.g: textbox1: Chicken, Textbox2: potatoes, etc.. on submit goes to a Create() post ActionResult which should find all the text boxes with the same class / value and store it to the recipe database in the same column which can then be retrieved when "ViewRecipe" is called.

A code snippet example would be great !

Thanks in advance !

vishal vazkar
  • 340
  • 1
  • 2
  • 15
  • What have you tried. What data model are you using? Which kind of database, how are the tables setup? Why a single column and not a child table as ingredients are clearly child records from a recipe table – Ric .Net Aug 01 '15 at 10:39
  • [This answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) gives you some options for dynamically generating collection items –  Aug 01 '15 at 13:16

1 Answers1

0

I would use foreign keys for this. As you said, a recipe has multiple ingredients. In Entity Framework this would be:

public class Recipe
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    // ...

    public virtual ICollection<Recipe> Recipies { get; set; }
}

public class Ingredient
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Amount { get; set; }
    public string Unit { get; set; }

    public virtual Recipe Recipe { get; set; }
    public int RecipeId { get; set; }
}

Like this you can have a recipe 'Pancaces for 2' with ingredients like '2 Eggs', '50g flour', '1/4 cup buttermilk', ...

Hope I understood your question correctly.

EDIT1:

I would not suggest to store all the ingredients in a single column, what's why I made an example for how to do it with foreign keys. How you would store this (supposed you're creating a new recipe) in a controller. It would be something like this:

public ActionResult Post(string recipyName, string[] ingredients)
{
    var recipy = new Recipy
    {
        Name = recipyName,
        Ingredients = ingredients.Select(x => new Ingredient
        {
            Name = x,
            Unit = "pcs.",
            Amount = 1,
        }).ToList(),
    }

    using (var context = new RecipyContext())
    {
        context.Recipes.Add(recipy);
        context.SaveChanges();
    }

    return Ok();
}

I really suggest not to store the ingredients in a single column. It is not extensible and you would have to do some kind of string trickery to get them out again. Like this everything is clear to everyone who reads your code.

EDIT2: I think this thread Asp.net razor textbox array for list items shows how to integrate such a model into the view rather nicely.

Community
  • 1
  • 1
LueTm
  • 2,366
  • 21
  • 31
  • HI LueTim, I wanted to know how the controller would pick all the values from each text box and save it in the database, e.g : I have 5 ingredients in the view and I click "Create" on post, how will the controller recognize and store all the textbox values in the same column in the database ? – vishal vazkar Aug 01 '15 at 10:57
  • This is almost what I want ! just one thing left here is I see that you are telling me to pass an array of ingredients. How will all the text box values for the ingredients get stored in this array ? again, a code snippet would be best and these examples are awesome. Thanks a lot once again for that – vishal vazkar Aug 01 '15 at 11:42
  • I'm not at home now, I'll reply as soon as I am! – LueTm Aug 02 '15 at 10:12