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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:23:02+00:00 2026-06-08T09:23:02+00:00

This is a follow up to a question that was asked yesterday. I have

  • 0

This is a follow up to a question that was asked yesterday.

I have a viewmodel, which shows a list of objectives. Using jquery I can add a new objectives line to the screen (the ID is set to 0 for any new objectives listed). When I click on the Save button to Post the objective list back to the controller, the controller loops through the objective list, and checks the ID against the database. If the ID is NOT found, it creates a new objective, adds this to the DB context, and saves te changes. It then retreives the ID, and returns the View(model) to the View.

The problem is, although the ID in the model, is updated to the database ID – when the model is rendered in the View again, it’s ID is still 0. So if I click Save again, it again, re-adds the “new objective added previously” to the database again.

My controller is shown below:

    //
    // 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) // if database row is found...
                {
                    objective.objective = item.objective;
                    objective.score = item.score;
                    objective.possscore = item.possscore;
                    objective.comments = item.comments;
                    db.SaveChanges();
                }
                else // database row not found, so create a new objective
                {
                    Objective obj = new Objective();
                    obj.comments=item.comments;
                    obj.objective = item.objective;
                    obj.possscore = item.possscore;
                    obj.score = item.score;
                    db.objectives.Add(obj);
                    db.SaveChanges();
                    // now get the newly created ID 
                    item.ID = obj.ID;
                 }
             }
        }
        return View(model);
    }

My ID is being set in the controller:

enter image description here

EDIT: Another example here, showing model.Objectives1.ID being updated:

enter image description here

However when the view renders it, it reverts to 0:

enter image description here

The Objectives list is determined as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcObjectives2.Models
    {
    public class ObjectivesEdit
    {
        public IEnumerable<Objective> Objectives { set; get; }
        public ObjectivesEdit()
        {
            if (Objectives == null)
                Objectives = new List<Objective>();
        }
    }
    }

The View has:

@model MvcObjectives2.Models.ObjectivesEdit

@using (Html.BeginForm())
{
  @Html.EditorFor(x=>x.Objectives)
  <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>
 }

and in my EditorTemplate (objective.cshtml):

@model  MvcObjectives2.Models.Objective          

<div class="objec">
<div>
@Html.TextBoxFor(x => x.objective})
</div>
<div>
@Html.TextBoxFor(x => x.score})
</div>
<div>
@Html.TextBoxFor(x => x.possscore})
</div>
<div>
@Html.TextBoxFor(x => x.comments})

@Html.HiddenFor(x => x.ID)  // This is the ID where it should now show the new ID from the database, but shows 0

</div>
</div>

I suspect the issue is somewhere in my controller – but I would appreciate any advise on how to get my View to render the new ID of the added objective.

  • 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-08T09:23:03+00:00Added an answer on June 8, 2026 at 9:23 am

    After rewording my search, I came across several posts which say this is by design. A Posted form expects to display what it sent to the controller, if the same page is shown again.

    However, you can add this, which will flush ModelState, and apparantly show the updated values from the model, updated in the controller:

            ModelState.Clear();
            return View(model);
    

    I’m not certain if this has any other effect yet – but for now, it appears to work ok.

    Thanks, Mark

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

Sidebar

Related Questions

I have a follow up question to this one . Now that I have
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
As a follow up to this question , I have a parent entity that
This is a follow-on question from the one I asked here . Can constraints
This is a follow-up to a question I asked yesterday. What I want to
this is actually a follow up to a question I asked yesterday about problems
So this is a follow-up to a previous question that I asked: Trying to
This question follows on from a question I asked yesterday about why using the
This question is a follow-up to a previous question that I had asked: How
this is a follow on question to a previously asked question . I have

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.