Reputation: 828
I had an MVC application to update a table in a database, but now I needed to add a ViewModel so that I could have the multiple tables displaying on a view page. But the problem now is that it will not save into the database now. I am using database first approach (not code-first).
Here is what I have so far:
In my ViewModel:
Public Class ClientUserProjectIssue
Public Property uTable As UserTable
Public Property issueType As IssueTypeTable
Public Property IssueTable As IssueTable
End Class
In the view:
@ModelType IssueTracker.ClientUserProjectIssue
@Html.EditorFor(Function(model) model.IssueTable.IssueSummary)
@Html.EditorFor(Function(model) model.IssueTable.IssueDescription)
In my controller:
Public Function UpdateIssue(issue As IssueTracker.ClientUserProjectIssue, objModelMail As IssueTable, command As String) As ActionResult
dbServer.Entry(issue).State = EntityState.Modified
dbServer.SaveChanges()
I will now get a null value because I am using the ViewModel instead of a single table.
I was trying to implement something like this:
Using context As New DbContext()
Dim dataModel As DatabaseEntities= context.IssueEntiteis.where(Function(x) x.Id = viewModel.Id).First()
dataModel.Name = viewModel.Name
dataModel.Type = viewModel.Type
context.SaveChanges()
End Using
But I was getting errors so I am pretty sure I can't do it that way because I am using database first, how can I implement a viewModel so that I can save tables in a database?
Upvotes: 0
Views: 188
Reputation: 146
You are passing the ViewModel to the DbContext.Entry method.
Instead of passing the ViewModel, you should pass the property of the ViewModel.
dbServer.Entry(issue.uTable).State = EntityState.Modified;
Upvotes: 1