Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:27:43+00:00 2026-06-08T21:27:43+00:00

This is my first MVC3 project and thought it’d be a bit simpler to

  • 0

This is my first MVC3 project and thought it’d be a bit simpler to accomplish this, but I’ve had a lot of issues. This seems to be the one trouble for which I’ve not encountered a solution. Posting here as a last resort.

My issue is that the Ingredients list never seems to populate in the database when a new Recipe is created. I’ve even tried hard-coding a list in the controller when the rest of the data is saved, but it still doesn’t do anything. As far as I can tell, there’s not even a field for Ingredients in the DB anywhere. Can anyone point out what I’m doing wrong? Much thanks.

Model:

public class Recipe
{
    public int ID { get; set; }

    [Required]
    public string Title { get; set; }

    [Required]
    [Display(Name = "Type of Meal")]
    public int TypeID { get; set; }

    [Required]
    public string Instructions { get; set; }

    [Display(Name = "Submitted By")]
    public string UserName { get; set; }

    public IList<string> Ingredients { get; set; }

    public virtual MealType Type { get; set; }
}

Create View:

@model final.Models.Recipe

@{
    ViewBag.Title = "Recipe Finder - New Recipe";
}

<h2>New Recipe</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    //adds an ingredient field on the fly
    var numIngredients = 0;
    function addIngredient() {
        $('#ingredientlist').append('<tr><td><input type="text" name="Ingredients[' + numIngredients++ + ']" value="' + $('#AddIngredient').val() + '" readonly="true" tabindex="-1" /></tr></td>');

        $('#AddIngredient').val('');
        $('#AddIngredient').focus();
    }

    function onKeyPress(e) {
        var keycode;

        if (window.event) {
            keycode = window.event.keyCode;
        }
        else if (e) {
            keycode = e.which;
        }
        else {
            return true;
        }

        //for addingredient field, add ingredient and not submit
        //  else mimic submit
        if (keycode == 13) {
            if (document.activeElement.id == "AddIngredient") {
                addIngredient();
                return false;
            }
            document.getElementById('btnSubmit').click();
        }

        return true;
    }

    //intercepts form submit instead of using submit button to disable addingredient textbox
    //  this prevents the value/field from posting
    function preSubmit() {
        $('#AddIngredient').attr('disabled', 'true');
        document.getElementsByTagName('form')[0].submit();
    }

    //intercepts users pressing the enter key
    if (document.layers) document.captureEvents(Event.KEYPRESS);
    document.onkeypress = onKeyPress;
</script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
<fieldset>
    <legend>by @User.Identity.Name</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.TypeID, "Type")
    </div>
    <div class="editor-field">
        @Html.DropDownList("TypeID", String.Empty)
        @Html.ValidationMessageFor(model => model.TypeID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Ingredients)
    </div>
    <div class="editor-field">
        @Html.TextBox("AddIngredient")
        <input type="button" value="Add Ingredient" onclick="addIngredient()" tabindex="-1" />
        <table id="ingredientlist">
        </table>
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Instructions)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Instructions, new { rows = "7", cols = "77" })
        @Html.ValidationMessageFor(model => model.Instructions)
    </div>

    @Html.HiddenFor(model => model.UserName)

    <p>
        <input type="button" value="Submit" onclick="preSubmit()" id="btnSubmit" />
    </p>
</fieldset>
}

Controller:

    [HttpPost]
    public ActionResult Create(Recipe recipe)
    {

        if (ModelState.IsValid)
        {
            db.Recipes.Add(recipe);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.TypeID = new SelectList(db.MealTypes, "ID", "Type", recipe.TypeID);
        return View(recipe);
    }

The POST fields I can see send all the information successfully, as seen below, I’m not sure what the issue is, or at this point what I haven’t already tried.

Title:test recipe
TypeID:2
Ingredients[0]:test0
Ingredients[1]:test1
Ingredients[2]:test2
Ingredients[3]:test3
Instructions:this is a test
UserName:tym
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:27:44+00:00Added an answer on June 8, 2026 at 9:27 pm

    You may want to check this page about storing a list of strings associated with an entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
I have an MVC3 project I created using the Code First paradigm and it
I just created a new project in MVC3 using EF4 code first deployed on
I am trying to write some unit tests for my MVC3 project (the first
I'm developing MVC3 project and have this situation. only at runtime. I'm using the
Im working on a MVC3 project using code first entity framework. On a webpage
I've recently added Microsoft Unity to my MVC3 project and now I'm getting this
First some context: I have an MVC3 .net project which, for the sake of
I had to take over an MVC 3 project from another developer. One of
I am creating a very simple EF4 code first project with one entity. public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.