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 8253881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:01:39+00:00 2026-06-08T01:01:39+00:00

I have a view that has a list of items (which can be added

  • 0

I have a view that has a list of items (which can be added to dynamically via jQuery).

When I POST the viewmodel back to the controller, if the code can’t find the ID, how do I insert new items and save them to the database.

My initial code is below – the updates are saved, but the new items aren’t saved:

    //
    // POST: /Objective/Edit/model
    [HttpPost]
    public ActionResult Edit(ObjectivesEdit model)
    {
        if (model.Objectives != null)
        {
             foreach (var item in model.Objectives)
            {
                // find the database row
                Objective objective = db.objectives.Find(item.ID);
                if (objective != null) 
                {
                    // Set the database row to the posted values

                    objective.objective = item.objective;
                    objective.score = item.score;
                    objective.possscore = item.possscore;
                }
                else  // database item not found, so add a new item
                {
                    // add a new objective 
                    // This doesn't seem to add/save a new record
                    Objective obj = new Objective();
                    obj.objective = item.objective;
                    obj.score = item.score;
                    obj.possscore = item.possscore;
                }
             }
            // Save the changes to the database
            db.SaveChanges();
        }
        return View(model);
    }

Thanks for any help,

Mark

  • 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-08T01:01:44+00:00Added an answer on June 8, 2026 at 1:01 am

    You don’t add the newly created objective to your context.

    else  // database item not found, so add a new item
    {
        // add a new objective 
        // This doesn't seem to add/save a new record
        Objective obj = new Objective();
        obj.objective = item.objective;
        obj.score = item.score;
        obj.possscore = item.possscore;
    
        // Missing line.
        db.objectives.Add(obj);
     }  
    

    if you’re using EF 4.0 (i.e. db is of type ObjectContext), you should use the db.AddObject(obj).

    Update based on your comment:
    One way is to retrieve all added items after saving changes. Another way is to modify your model when creating a new objective. Changed parts are marked with *:

    foreach (var item in model.Objectives.ToList()) // *:Notice the ToList()
    {
        // find the database row
        Objective objective = db.objectives.Find(item.ID);
        if (objective != null) 
        {
            // Set the database row to the posted values
    
            objective.objective = item.objective;
            objective.score = item.score;
            objective.possscore = item.possscore;
        }
        else  // database item not found, so add a new item
        {
              // add a new objective 
              // This doesn't seem to add/save a new record
              Objective obj = new Objective();
              obj.objective = item.objective;
              obj.score = item.score;
              obj.possscore = item.possscore;
    
             db.AddObject(obj)
             // Save the changes to the database
            db.SaveChanges(); // *: save in loop to get thee ID.
    
            item.ID = obj.ID; // *: assign the ID to the model.
         }
    }
    
     return View(model);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a View which has a list of items bound to my ViewModel
I have a list view that displays a collection of items, each item has
I have a list of tab items that have views dynamically added to them.
I have a picker view that has a list of numbers to pick from.
Overview: I have an Expandable List View which displays items of the type ToDoElement
So I have a view model call ProductViewModel which has a list of sites
I have list of items in my activity which I load dynamically, item after
I have a list view which has been registered for a context menu. For
I have a view that has a column to make it sortable. When clicking
I have a view that has a tooltip attribute. I want to set that

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.