Add model with link to model object EFModelFirst
So I have 2 models: Person and Ingredient and the thing I want to accomplish is that anyone can choose an ingredient (through a jQuery autocomplete), input their data and then on create it saves that to the database.

So it would be like this:

        public class Person {
        
                public int PersonID { get; set; }
        
                [Required(ErrorMessage="Given name is a required field")]
                [DisplayName("Given name")]
                public string GivenName { get; set; }
        
                [Required(ErrorMessage="Family name is a required field")]
                [DisplayName("Family name")]
                public string FamilyName { get; set; }
        
                [Required(ErrorMessage = "Birthdate is a required field")]
                [DisplayFormat(DataFormatString = "{0:d}")]
                public DateTime Birthdate { get; set; }
        
                [DisplayName("Ingredient")]
                public virtual Ingredient Ingredient { get; set; }
        }

And:

    public class Ingredient {
        public int IngredientID { get; set; }

        [Required(ErrorMessage = "Ingredient is a required field")]
        [Remote("IngredientExists", "Person")]
        public string Name { get; set; }

        public virtual ICollection<Person> Persons { get; set; }
    }

Now my view currently looks like this:
(snippet for just the ingredient part)

		<div class="editor-label">
        @Html.LabelFor(model => model.Ingredient)
    </div>
		<div id="ingredients-list" class="editor-field">		
        @Html.EditorFor(model => model.Ingredient.Name)
			@Html.ValidationMessageFor(model => model.Ingredient.Name)
			@ViewBag.IngredientError
    </div>

Controller:

    //
    // POST: /Person/Create

    [HttpPost]
    public ActionResult Create(Person person) {
        if (ModelState.IsValid) {
            db.People.Add(person);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        person.Ingredient.Name = "";

        return View(person);
    }

Now this all works except for 1 thing (a kinda major issue), when I select an ingredient from my dropdown, it saves the person AND creates a new ingredient in the database with a new ID and the same value as the one chosen (ie: a duplicate).
Now, I'm quite new to the EF and LINQ so I haven't figured out how to do it just yet. Any ideas/solutions will be greatly appreciated!
Add model with link to model object EFModelFirst
===

So I have 2 models: Person and Ingredient and the thing I want to accomplish is that anyone can choose an ingredient (through a jQuery autocomplete), input their data and then on create it saves that to the database.

So it would be like this:

        public class Person {
        
                public int PersonID { get; set; }
        
                [Required(ErrorMessage="Given name is a required field")]
                [DisplayName("Given name")]
                public string GivenName { get; set; }
        
                [Required(ErrorMessage="Family name is a required field")]
                [DisplayName("Family name")]
                public string FamilyName { get; set; }
        
                [Required(ErrorMessage = "Birthdate is a required field")]
                [DisplayFormat(DataFormatString = "{0:d}")]
                public DateTime Birthdate { get; set; }
        
                [DisplayName("Ingredient")]
                public virtual Ingredient Ingredient { get; set; }
        }

And:

    public class Ingredient {
        public int IngredientID { get; set; }

        [Required(ErrorMessage = "Ingredient is a required field")]
        [Remote("IngredientExists", "Person")]
        public string Name { get; set; }

        public virtual ICollection<Person> Persons { get; set; }
    }

Now my view currently looks like this:
(snippet for just the ingredient part)

		<div class="editor-label">
        @Html.LabelFor(model => model.Ingredient)
    </div>
		<div id="ingredients-list" class="editor-field">		
        @Html.EditorFor(model => model.Ingredient.Name)
			@Html.ValidationMessageFor(model => model.Ingredient.Name)
			@ViewBag.IngredientError
    </div>

Controller:

    //
    // POST: /Person/Create

    [HttpPost]
    public ActionResult Create(Person person) {
        if (ModelState.IsValid) {
            db.People.Add(person);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        person.Ingredient.Name = "";

        return View(person);
    }

Now this all works except for 1 thing (a kinda major issue), when I select an ingredient from my dropdown, it saves the person AND creates a new ingredient in the database with a new ID and the same value as the one chosen (ie: a duplicate).
Now, I'm quite new to the EF and LINQ so I haven't figured out how to do it just yet. Any ideas/solutions will be greatly appreciated!