I have a Person class. A person has a name and a salutation. Salutation is another table in the database (I inherited the database).
In the UI, I want the Salutation to be a required field. Here’s what my model looks like:
public partial class Person
{
public virtual string Id { get; set; }
public virtual Nullable<long> SalutationId { get; set; }
[Required]
public virtual Salutation Salutation { get; set; }
public virtual string FirstName { get; set; }
[DisplayName("Last Name")]
public virtual string LastName { get; set; }
}
public partial class Salutation
{
public virtual long Id { get; set; }
public virtual string SalutationName { get; set; }
public virtual string Status { get; set; }
public virtual ICollection<Person> People { get; set; }
}
Is this correct? I am still getting a validation error that says “Salutation is a required field,” even when I have entered Salutation.
All this started just an hour ago, until when everything was working fine.
I would’ve thrashed the issue out myself if I had time to think and wasn’t chasing a deadline. Strange what pressure does to your mind.
Short Answer: Issue is in the place where you put required validation for your Salutation.
Your salutation validation attributes should be placed inside the
Salutation model.Edit: in addition to that your model is using
public virtualaccess modifier on each property. I would removevirtualif my model don’t need that.