1

I have a simple Model/Controller setup (in ASP.NET MVC) for a "Manager" class, which contains information about a client, including a password field. Instead of having a whole separate controller/view for updating the password, I want the edit form for a Manager record to update the password field ONLY when a new password is entered. (Assume that it is an Admin-type user who is editing the Manager records).

In the code below, I took out the attributes for the other fields to save space...

public class Manager
{
    public int ManagerID { get; set; }
    public string ManagerName { get; set; }
    public string Phone1 { get; set; }
    public string Phone2 { get; set; }
    public string Email { get; set; }
    public string OrganizationName { get; set; }
    public string OrganizationType { get; set; }
    public string Notes { get; set; }
    public bool Active { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Update Password")]
    public string Password { get; set; }
}

My Manager View and the ManagersController both have the default generated code. The Edit method definition is:

public async Task<IActionResult> Edit(int id, [Bind("ManagerID,ManagerName,Phone1,
    Phone2,Email,OrganizationName,OrganizationType,Notes,Active,Password")]
    Manager manager)
{
    .
    .
    .
    _context.Update(manager);
    await _context.SaveChangesAsync();
}

So at this point, if I leave the password blank, it of course, updates the record with a null value. I need to do two things: 1. Check to make sure that the Password value is not null, and 2. If it is not null, allow it to update, otherwise STOP the password value from being updated in the table, but then allow the rest of the fields to update.

As you can see, the Password field is bound. I'm wondering if I need to bind it manually, or unbind it when it is null, or is there something in the class definition that would help?...I just don't know.

2 Answers2

1

Try using a good ol' backing field. Auto properties are great when there's no logic involved, but in this case, I think a simple check if the value passed is null before updating the field can do the job:

private string password;

[DataType(DataType.Password)]
[Display(Name = "Update Password")]
public string Password 
{ 
    get
    {
        return password;
    }
    set
    {
        if (!string.IsNullOrEmpty(value))
        {
            password = value;
        }
    }
}
Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
  • Making that change alone didn't seem to solve it...is there somewhere else that I need to change? Do I have to change "Password" to "password" in some places? I edited a Manager record, left the password blank, and it still updated it to null. – Jack Thomson Oct 13 '17 at 15:57
  • Is it still updating with the `null` value? Maybe it's an empty string, not `null`. I updated the answer. – Anderson Pimentel Oct 13 '17 at 17:31
  • This works for updating the instance of the class (I think), but not for avoiding the null value being saved into the database table. Your answer DID help me figure out what to do. I have posted my solution. – Jack Thomson Oct 13 '17 at 17:34
  • Good to know you solved it! I have little to zero knowledge about `EntityFramework`: I'm a `NHibernate` man! =D BTW, you can (and should, in this case) accept your own answer. – Anderson Pimentel Oct 16 '17 at 11:16
1

I found another question that was similar, and I derived an answer from it:

Field is updating with null value...

So many properties to deal with! (I'm used to lower level languages). Just before the call to save the data, if the password is null, set the IsModified property for the Password field to be false. Then it won't update the field in the database:

_context.Update(manager);

if (manager.Password == null)
{
    _context.Entry(manager).Property(x => x.Password).IsModified = false;
}

await _context.SaveChangesAsync();

I'm just a noob here, but it did seem important that the IsModified being set to false had to be after the _context.Update(manager); command.

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54