What is the design paradigm to use when one wants to fill in some model values from the 'backend'? e.g. I have movies DB; users can create movies. I want to record who created a movie, CreateOn, LastModified. My model has UserId, CreatedOn, LastModified, ... (name, genre etc). The default view generated has all these fields; I removed CreatedOn, LastModified, UserId from create view; now when things come back to Create function in controller, the model is invalid. I cleared the model errors via Clear function, then filled in these 3 values in the controller. But then this is not right; the Clear clears off any other validation errors as well.
public ActionResult Create([Bind(Include = "Name,Description,Genre")] Movie movie)
{
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
ModelState.Clear();
movie.UserId = user.Id;
movie.CreatedOn = DateTime.UtcNow;
movie.LastModified = DateTime.UtcNow;
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}