Search code examples
c#asp.net-coreentity-framework-coreasp.net-core-mvc

Updating an Entity Record not Found


I'm having a hard time understanding why my entity state not updating. The goal is to have the a client details updated. What I have done so far is have a form that populates the client data in inputs for change.

Once a field is changed and the user hits submit it should hit the HTTP post and process the change entity to the database, however, I get this error but I know that the record is present and the UpdateDetails page is populating the inputs with the correct data.

What I did notice is that when debugging the Client ID is passing 0 when it hits the HTTP post. Why is that happening? I have no idea.

I do have a working method for products and it is essentially doing the same process as the client but the product id is present. What am I overlooking here?

Technologies being used is ASP.NET Core 8 MVC.

DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.

UpdateClient.cshtml:

<div class="row">
    <form asp-action="UpdateClient" asp-controller="Admin" method="post">
        <div class="row">
            <div class="col-4 mb-3">
                <label asp-for="CompanyName">Company</label>
                <input asp-for="CompanyName" class="form-control" />
                <span class="help-block text-danger" asp-validation-for="CompanyName"></span>
            </div>
            <div class="col-4 mb-3">
                <label asp-for="BranchName">Branch Name</label>
                <input asp-for="BranchName" class="form-control" />
                <span class="help-block text-danger" asp-validation-for="BranchName"></span>
            </div>
            <div class="col-4 mb-3">
                <label asp-for="PhoneNumber">Phone Number</label>
                <input asp-for="PhoneNumber" class="form-control" />
                <span class="help-block text-danger" asp-validation-for="PhoneNumber"></span>
            </div>
        </div>
        <div class="mb-3">
            <label asp-for="Address">Address</label>
            <input asp-for="Address" class="form-control" />
            <span class="help-block text-danger" asp-validation-for="Address"></span>
        </div>
        <div class="mb-3">
            <label asp-for="ShippingAddress">Shipping Address</label>
            <input asp-for="ShippingAddress" class="form-control" />
            <span class="help-block text-danger" asp-validation-for="ShippingAddress"></span>
        </div>
        <div class="form-group mb-3">
            <button type="submit" class="btn btn-primary">Update</button>
        </div>
    </form>
</div>

AdminController.cs:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateClient(Client client)
{
    _clientRepository.UpdateClient(client);
    return RedirectToAction("Dashboard", "Admin");
}

ClientRepository.cs:

public void UpdateClient(Models.Client client)
{
    var result = _dbContext.Entry(client).State = EntityState.Modified;
    await _dbContext.SaveChangesAsync();
}

When debugging this is what I mean that the client ID is showing an "id" of 0:

Test Client Data


Solution

  • Answering my own question. I forgot to pass the ClientId in the form so what I changed is from doing this

    
    <div class="row">
        <form asp-action="UpdateClient" asp-controller="Admin" method="post">
    
            // Other fields
    
            <div class="form-group mb-3">
                <button type="submit" class="btn btn-primary">Update</button>
            </div>
        </form>
    </div>
    
    

    to this

    
    <div class="row">
        <form asp-action="UpdateClient" asp-controller="Admin" method="post">
            <input hidden asp-for="ClientId" /> <-- Needed for db detect which entity to change
    
            // Other fields
    
            <div class="form-group mb-3">
                <button type="submit" class="btn btn-primary">Update</button>
            </div>
        </form>
    </div>