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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:54:17+00:00 2026-05-24T02:54:17+00:00

So I have this demo project almost completely working. public class Project { public

  • 0

So I have this demo project almost completely working.

public class Project
    {
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        public virtual ICollection<Task> Tasks { get; set; }

    }

    public class Task
    {
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        public int ProjectID { get; set; }

        public virtual Project Project { get; set; }

    }

Controller

public ActionResult Edit(int id)
        {           
            var project = db.Projects.Where(p=>p.ID==id).Single();
            return View(project);
        }

        [HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                var dbProject = db.Projects.Where(p => p.ID == project.ID).Single();

                UpdateModel(dbProject);
                db.SaveChanges();                
                TempData["Success"] = "Modelo Valido";
            }
            return RedirectToAction("Index");
        }

View//strongly typed for project

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Project</legend>
        @Html.HiddenFor(model => model.ID)
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <h1>Tasks</h1>
        @Html.EditorFor(m => m.Tasks)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

EditorTemplate

@model MvcApplication2.Models.Task
<span>Task</span>
<br />
    @Html.LabelFor(m => m.Name)
    @Html.EditorFor(m => m.Name)
    @Html.HiddenFor(m => m.ID)
    @Html.HiddenFor(m => m.ProjectID)
    @Html.ValidationMessageFor(m => m.Name)

The view displays this

enter image description here

The problem is that when I submit the form the Tasks are populated with everything except the virtual Project property… so the error i get it is

The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.

Here is a pic of my debugging breakpoint result

enter image description here

Please Help.

UPDATE:

I have changed my controller action to this

[HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();                
                TempData["Success"] = "Modelo Valido";
                return RedirectToAction("Index");
            }
            return View(project);
        }

it is still not working correctly.
Now changes made to the Name of the project are updated correctly in the database. but changes made to any Task Name are ignored completely.

  • 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-05-24T02:54:17+00:00Added an answer on May 24, 2026 at 2:54 am

    I have found a way to get this to work but Im not completely happy with the approach.

    see this question on how to refactor my current code to see how I am currently (hopefully temporarily doing it)

    Help improving (refactoring) my code. Automapper – EF – asp.net mvc-3

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

Sidebar

Related Questions

Take this example model: public class Location { public string Id { get; set;
I have been working on a demo project all day and everything went just
see this demo from jquery ui you have to hold down the Ctrl key
Please have a look at this demo page that I cooked up a while
I am trying to implement this demo on my local machine. http://bit.ly/4g4o1r I have
I have been playing with the demo code from this msdn article by Jeffrey
http://demo.thethemefoundry.com/traction/#post-183 Which wordpress plugin is this, to have post image on the left side
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have made the smallest demo project to illustrate my problem. You can download
I have a client table (with fields id, name) and a project table (with

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.