I am creating an MVC 3 website. I have a model that looks like this:
public class Survey { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } // Not in view public DateTime DateCreated { get; set; } // Not in view public DateTime DateModified { get; set; } }
Based on this, I also have a viewing model for editing survey information:
public class SurveyEditViewModel { public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } }
When the user finishes editing, I would like to save the changes. Here is my postcontrol action:
[HttpPost] public ActionResult Edit(SurveyEditViewModel model) {
In my repository (so far this is generic), I have the following code:
public void Update(E entity) { using (ABCDataContext context = new ABCDataContext()) { context.Entry(entity).State = System.Data.EntityState.Modified; context.SaveChanges(); } }
When this is done, I get the following error: "A statement about updating, inserting or deleting affected an unexpected number of rows (0). Objects can be changed or deleted from the moment objects are loaded. Update ObjectStateManager records.
I think this should be expected. Matching a view model with a model does not give me the complete Survey object.
I could change my controller to look like this. And then it works:
[HttpPost] public ActionResult Edit(SurveyEditViewModel model) {
But I was wondering if a better way is available?