-1

I'm new to ASP.NET MVC so bear with me.

I have a model "profile" which holds date registered, username and about me. When someone edits their profile (Which I made using entity framework and scaffoling) I don't want them to be able to update the register date, so I took it out of the view, but now when I save changes, it says it can't save a null date.

How do I tell ASP.NET MVC that I don't want the user to be able to update that field?

Thanks in advance.

  • The simplest way is to either make the DateRegistered read-only, or hidden. The best way is to create a ViewModel which has a read-only DateRegistered field and use that for your View instead of your actual Model. – rikitikitik Aug 11 '15 at 09:58
  • Use a view model with only the properties you need to display/edit in the view. In the POST method, get the data model, map the view model properties to the data model and save the data model. Refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Aug 11 '15 at 09:59
  • Thanks guys, I'd read up on ViewModels but wasn't sure if that was the right route to take. I'll take a good look, thanks! – Exception_i Aug 11 '15 at 10:06
  • Why on earth have you accepted a wrong answer? All using `[Editable(false)]` does is set the `IsReadOnly` property ModelMetadata. It does not solve your issue. –  Aug 11 '15 at 12:02
  • @StephenMuecke Woah, calm down. I didn't know it was wrong, that's why I'm asking. I've unmarked it for you. – Exception_i Aug 11 '15 at 13:07
  • SO is a site for the community. Other coming across this question would have assumed it was the correct answer and solved the problem because you accepted it, and may have wasted hours before realizing it was wrong. You should only accept answers that you have tested and confirmed are correct. –  Aug 11 '15 at 13:11
  • Ruining SO's community by attacking new users is a great approach to commenting on a site for the community. – JDupont Aug 11 '15 at 13:58

1 Answers1

-1

Hey you can use the following attribute above your registerdate in your model

[Editable(false)]
Chino
  • 821
  • 6
  • 13
  • Still getting errors but I think this might help with the situation, hours of searching and I found nothing about this, thank you very much. – Exception_i Aug 11 '15 at 10:06