In my code I have the following:
viewModel.Note.Modified = DateTime.Now;
viewModel.Note.ModifiedBy = User.Identity.Name;
and in my view:
[DisplayName("Modified")]
public DateTime Modified { get; set; }
[DisplayName("Modified By")]
public string ModifiedBy { get; set; }
Could I just have my code change ModifiedBy and then have some code that runs in the ViewModel that changes the date of Modified when the Modified by is changed?
If
viewModel.Noteis a reference to aNoteViewModelinstance, then you can use the following:Then in your “code” (I’m guessing you meant the controller?), you can just do:
and you will have the intended result.
Side Note: Depending on the audience of your application, you may want to consider using
DateTime.UtcNowfor localization purposes.DateTime.Nowwill return the current DateTime on the server, which is dependent on the server’s location. If you are displaying this data to the user, it is likely that you will want to either (a) specify the time zone, or (b) localize the time to the time zone of the client machine