0

I am a beginner to MVC5 and Entity Framework, so please be gentle. I've searched endlessly for something related to this question to no avail, maybe because I don't understand the problem well enough.

The issue that I'm having is that when I 'Edit' my entries from my View, values that aren't explicitly set from within the Form end up getting set to 'NULL' in my database after db.SaveChanges() is called.

Two values which were set at time of Creation, Email and Date_Submitted, are being set to 'null'. Ideally, these values would not be changed when in Edit mode, I would like for them to remain set.

I tried changing everything I could think of with no luck, and creating a new controller from scaffolding doesn't give me any answers. What am I missing? Suggestions?

  • Use a view model containing only those properties you need to edit in the view. Refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). Post back the view model, get the data model from the database, update its properties from the view model and save the data model. –  Nov 01 '15 at 04:17

1 Answers1

0

You shouldn't add unused parameters to the edit controller. If you remove the parameters you don't use in the edit view (eg email), the values aren't changed by this controller

Luc
  • 1,491
  • 1
  • 12
  • 21
  • Thank you Luc for your reply. Are you referring to the `public ActionResult Edit([Bind(Include = ...` section of my controller? I removed 'email' and 'date_submitted' from the includes, but it is still setting those values to null in my database after post. Anything else that you can think of? – Angelo Veto Oct 31 '15 at 19:36
  • A stupid solution would be to add those fields (email, date_submitted) as hidden fields – Luc Oct 31 '15 at 19:39
  • Wow, so the stupid solution seems to have worked. Would the non-stupid solution be to rewrite all of my code? – Angelo Veto Oct 31 '15 at 19:49