I have a Student class in my application as below:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string RoleNum { get; set; }
public DateTime RegistrationDate { get; set; }
public DateTime AdmissionDate { get; set; }
}
Now I have a few views in the application which update the student model. But not every view needs to update every field of the student table in database. e.g. the registration date is set only once when the student is first created. Now the edit student view should not again update the RegistrationDate.
The problem is that RegistrationDate is the required field in the database so not including that in the view form generates the exception of having NULL in RegistrationDate.
So to prevent that, I am hiding the RegistrationDate fields in a div so it is not visible in the form. Is that the right way of doing this thing or am I missing a very simple way?
When updating an entity the idea is to first fetch the entity from the database that that you want to update using the ID and then use the
TryUpdateModelmethod to update only the fields that were part of the original request and finally save the model.Here’s a commonly used pattern for updating an entity: