Only two properties of the model are updated

I defined entity presented below:

public class Customer
{
    public int CustomerId { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string PhoneNumber { get; set; }
    [Required]
    public string Email { get; set; }
    public ICollection<Call> Calls { get; set; }
}

I am trying to updated fields of the entity in the controller method:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("CustomerId,FirstName,LastName,PhoneNumber,Email")] Customer customer)
{
    if (ModelState.IsValid)
    {
        _context.Update(customer);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(customer);
}

All values of the model have changed in the view and have been passed correctly to the method. ModelState is valid. All of the methods are executed, but after all in the database only FirstName and LastName are changed. Other properties not. I checked SQL which is executed:

UPDATE [Customers] SET [FirstName] = @p0, [LastName] = @p1
WHERE [CustomerId] = @p2;

Why other properties are not updated?

View Code:

<h4>Customer</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="CustomerId" />
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="PhoneNumber" class="control-label"></label>
                <input asp-for="PhoneNumber" class="form-control" />
                <span asp-validation-for="PhoneNumber" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

1 answer

  • answered 2018-01-13 17:31 Mr.AF

    but after all in the database only FirstName and LastName are changed

    Below sql statement is not just updating Firstname and Lastname .

    UPDATE [Customers] SET [FirstName] = @p0, [LastName] = @p1
    WHERE [CustomerId] = @p2;
    

    if you want other fields to be updated you must SET other parameters too.