Say, I have a model Car with these properties (for simplicity, I skipped get/set):
string Name; // user can change
string Secret; // user cannot change
DateTime CreatedAt = DateTime.Now; // even we cannot change
Then in ASP Web Application 3.1, the scaffold generates Edit.cshtml.cs with this contents (simplified):
[BindProperty]
public Car Car { get; set; }
public Task<IActionResult> OnPost()
{
_context.Attach(LanguageExporter).State = EntityState.Modified;
_context.SaveChanges();
// redirect ...
}
So it allows a user to make POST with all the fields of Car.
But actually, I would like to ignore CreatedAt completely and not allow changing Secret from POST parameters (but allow from code).
[BindNever] does not help since ASP skips Secret and it keeps default(string) => null on it which I don't want. For CreatedAt it keeps default(DateTime) => 01.01.0001.
So I am appealing to the community with the question, how can I automate the process of editing my models?
As I see it, ASP should ignore some POST parameters and EF should ignore updating some properties. Or ASP should fetch the model first and change only particular properties (e.g. keep CreatedAt).
What are the best practices out there in this situation?