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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:23:05+00:00 2026-06-01T17:23:05+00:00

I have a code first database. The tables causing me issues are public class

  • 0

I have a code first database. The tables causing me issues are

public class Deadline
{
    public Deadline() { }
    public Deadline(Year year)
    {
        this.Year = year;
    }

    public Guid Id { get; set; }

    [Required]
    public Year Year { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DataAnnotationsExtensions.Date]
    public DateTime From { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DataAnnotationsExtensions.Date]
    public DateTime To { get; set; }
}

public class Year
{
    public Year(Int32 value) { this.Value = value; }

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [Required]
    [Display(Name="Year")]
    [DataAnnotationsExtensions.Year]        
    public Int32 Value { get; set; }

    public virtual Deadline Deadline { get; set; }
}

I have a controller for the year which has the create get method

    public ActionResult Create()
    {
        ViewBag.Id = new SelectList(db.Deadline, "Id", "Id");
        var Year = new Year(db.Year.OrderByDescending(x => x.Value).FirstOrDefault().Value + 1);
        Year.Deadline = new Deadline(Year);
        return View(Year);
    } 

and the post method

    [HttpPost]
    public ActionResult Create(Year year)
    {
        if (ModelState.IsValid)
        {
            if (db.Year.Select(x => x.Value).Contains(year.Value))
            {
                ModelState.AddModelError("AlreadyExists", "That year has already been added to the database, please edit instead");
                return View(year);
            }
            db.Year.Add(year);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(year);
    }

Whenever i try to create a new Year though (having set the year value and the deadline dates) it gets back to the post and the modelstate is invalid and says

The Year field is required.

I’ve checked and at the post method year.Deadline.Year is indeed null, what i want to know is why (and how i can fix this) it’s been driving me crazy, even putting

@{ Model.Deadline.Year = Model; }

in the view did nothing.

Edit The following works

    using (ProjectSelectionAndAllocation.Models.DatabaseContext Context = new Models.DatabaseContext())
    {
        var h = new Models.Year(1001);
        h.Deadline = new Models.Deadline(h);
        Context.Year.Add(h);
        Context.SaveChanges();
    }
  • 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-01T17:23:06+00:00Added an answer on June 1, 2026 at 5:23 pm

    Ok, I have two basic theories as to the problem but one solution that you should implement anyway.

    So I’ll start with the solution: Use a ViewModel. That is a small, flat, lightweight class that just contains the fields the view wants to display. It should not be the same as your data layer. The view should just be rendering stuff, and the ViewModel should just contain the data to be displayed.

    You’ll map the data from your actual objects to the ViewModel (AutoMapper is pretty popular for this.)

    Now, why will this help? Well, my theories as to your problem are:

    1) The model binder might not like virtual properties. Whenever you return a form to a function in MVC there’s little bit of code that runs through and tries to map all the values in the form to their fields. This model binder might be unable to figure out a virtual field or there might be some good reason not to automatically map to one. Either way, a ViewModel would replace this with a regular public property.

    2) The other possibility, but I’m not too sure of it, is that there is a circular reference (Year -> Deadline -> Year ) and the model binder doesn’t handle that well. Again though, the ViewModel concept would get rid of that and you’d just have a Year field in the ViewModel.

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

Sidebar

Related Questions

I currently have this code which reads the first field in a database record
I have an issue in entity framework code first, three table in database as
I have an Entity in Code First Entity framework that currently looks like this:
I have a couple of classes (for this example anyway) that use code first
I have a database that was created with the Entity Framework's code first approach.
I'm trying to get Entity Framework Code First to create a database for me
I have database with many tables. In the first table, I have a field
I am getting this error when generate Database from Entity Framework Code First. I
I have some code that first selects an option value in a dropdown menu
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,

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.