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.