Napoleon
Napoleon

Reputation: 388

Unable to save model back to database

I'm making a simple edit operation for my Project model in a RazorPage. I can read the data and edit the data in the model but I'm unable to save it back into the database.

It's as if the public bound Project variable is suddenly no longer linked to the database-context anymore. In the OnPost(), the bound Project variable doesn't seem to be the same as the one from await _context.Project.FirstAsync(p => p.Id == project.Id); anymore?

Code:

     public class EditProjectModel : PageModel
        {
            [BindProperty]
            public Project Project { get; set; }
    
            ...
    
            private async Task SaveProjectChanges(Project project) // How to save this parameter back into the database?
            {
                Project dbProject = await _context.Project.FirstAsync(p => p.Id == project.Id);
                // How to save the changes from project?
                // I don't want to assign each property manually like so: dbProject.Name = project.Name
                // dbProject = project; // Erm yeah... This is obviously pointless unless I can use a pointer or something... But there's got to be a better way.
                await _context.SaveChangesAsync();
            }
    
            public async Task<IActionResult> OnPost()
            {
                if (Project.Id > 0)
                {
                    await SaveProjectChanges(Project);
                }
                else
                {
                    await AddNewProject(Project);
                }
    
                return RedirectToPage(<snip>);
            }

Snipped from the view if it matters:

    <tr>
        <td>Id</td>
        <td>@Model.Project.Id<input type="hidden" name="Project.Id" value="@(Model.Project.Id)" /></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><input type="text" name="Project.Name" value="@(Model.Project.Name)" /></td>
    </tr>

    ...

    <input type="submit" value="Save" class="btn btn-default" />

Upvotes: 0

Views: 148

Answers (1)

pyordanov11
pyordanov11

Reputation: 870

When calling your SaveProjectChanges(Project project) method you're passing the model as an input parameter, but inside you're not using it. You're right in that the object you're pulling with Project dbProject = await _context.Project.FirstAsync(p => p.Id == project.Id); is not the same as the input parameter, it's another instance.

In order to update the database record you need to use the Update() method of your DbContext as _context.Update(project); - official docs

So your SaveProjectChanges method should end up looking something like this:

private async Task SaveProjectChanges(Project project)
{
    _context.Update(project);
    await _context.SaveChangesAsync();
}

Note: Since your Project input parameter is in a "disconnected" state, make sure that its database key field is set, otherwise you'll end up with a new record in the database instead of updating the existing one.

Upvotes: 1

Related Questions