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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:26:31+00:00 2026-06-18T03:26:31+00:00

I am studying asp.net mvc with Code First…. I have a class called Livro:

  • 0

I am studying asp.net mvc with Code First….

I have a class called Livro:

Here its the code

    public class Livro
    {

    [Key]
    public int LivroId { get; set; }

    [Required(ErrorMessage = "E necessario titulo")]
    [MaxLength(100, ErrorMessage = "Titulo deve ter no maximo 100 caracteres")]
    public string Titulo { get; set; }


    public int AutorID { get; set; }

    public virtual Autor Autor { get; set; }
  }

As you can see it has a Navigation property called Autor and FK called AutorId. But I have this code on the Livro controller (I didn’t write this code, VS created based no my class).

    [HttpPost]
    public ActionResult Create(Livro livro)
    {
        if (ModelState.IsValid)
        {
            db.Livros.Add(livro);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.AutorID = new SelectList(db.Autores, "AutorId", "Nome", livro.AutorID);
        return View(livro);
    }

If i have [Required] on the Autor attribute, the modelState.IsValid becomes false because livro.Autor is null. So i must take it out.

But i was reading a book from Julia Lerman that its called “Programming EF Code-First” and
sometimes there is navigation properties with the [Required] attribute.

What am i missing here?

  • 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-18T03:26:32+00:00Added an answer on June 18, 2026 at 3:26 am

    It’s probably because the View only refers to the Livro fields, not the Autor and the model binder is unable to create the Autor navigation property. You can create fields for the Autor, using smth like this:

    @Html.TextBoxFor(m => m.Autor.AutorID);
    @Html.TextBoxFor(m => m.Autor.AutorName);
    

    However, this will become painful and maybe you only need to select the author from the list. I would add the Required attribute to the Livro.AutorID property only and leave Autor as is.

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

Sidebar

Related Questions

Studying asp.net mvc 3 + EF code-first. I am new to both. My example
I am studying Code First EntityFramework together with ASP.Net MVC 3. At first my
I have started to learn ASP.NET MVC, and at this time of studying I
I have been studying, playing with and working with ASP.NET MVC since Preview 1
I'm studying ASP.NET MVC and I use LINQ to SQL for model. I have
I am trying to teach ASP.NET MVC to students (undergrads) that have been studying
I'm currently studying asp.net mvc and I just started, I decided to move away
I'm currently studying asp.net mvc and I just started, I decided to move away
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
I download asp.net mvc2 preview 2 days back. I've just started studying MVC. I

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.