I have a few models in my MVC3 web app that have fields that need to be set “behind the scenes” when a user creates or edits an object/entity.
I’m trying to figure out what the best practice is regarding these types of fields.
For example…
public class EntityA {
public int Id { get; set; }
public string Title { get; set; }
...
[ForeignKey("User")]
public int UpdatedBy_Id { get; set; }
public virtual User UpdatedBy { get; set; }
}
The create and edit views for this allow the user to edit the “Title” field, but the “UpdatedBy” field needs to be set by the app when the entity is inserted or updated.
Is it best to drop a hidden field on the views and set “UpdatedBy_Id” there, or use the model property “get/set” body to do so? …or… Should this be on the HttpPost in the controller?
I’d prefer to place fields like this outside of user control. Especially if they’re integer fields a user can edit to make phony records. The choices then fall between using TempData(if session is enabled) or possibly retrieving it on the fly for the current user. If you’re not worried about the user modifying them, then I’d go with a simple hidden field or placing it in the route values for the post, allowing the framework to do the work for you.