Hi,
Any suggestions on how to improve this code which updates user role (MVC).
Thanks
[Authorize(Roles = "Administrator")]
[HttpPost]
public ActionResult EditUser(UserAdminViewModel model)
{
var result = m_AccountModel.UpdateUser(model);
if (result)
return RedirectToAction("UserList");
}
public bool UpdateUser(UserAdminViewModel model)
{
using (var dbConnection = MyDbConnectionPool.GetConnection())
{
var user = dbConnection.Query<DbAspNetUserRoles>().FirstOrDefault(p => p.UserId == model.Id);
if (user == null)
{
var newRoleRec = new DbAspNetUserRoles()
{
RoleId = model.Role,
UserId = model.Id
};
dbConnection.Insert(newRoleRec);
}
else
{
var sql = $"UPDATE AspNetUserRoles SET RoleId = '{model.Role}' WHERE UserId = '{model.Id}'";
dbConnection.Query<DbAspNetUserRoles>(sql).ToList();
}
}
}
Any suggestions on how to improve this code which updates user role (MVC).
Thanks
[Authorize(Roles = "Administrator")]
[HttpPost]
public ActionResult EditUser(UserAdminViewModel model)
{
var result = m_AccountModel.UpdateUser(model);
if (result)
return RedirectToAction("UserList");
}
public bool UpdateUser(UserAdminViewModel model)
{
using (var dbConnection = MyDbConnectionPool.GetConnection())
{
var user = dbConnection.Query<DbAspNetUserRoles>().FirstOrDefault(p => p.UserId == model.Id);
if (user == null)
{
var newRoleRec = new DbAspNetUserRoles()
{
RoleId = model.Role,
UserId = model.Id
};
dbConnection.Insert(newRoleRec);
}
else
{
var sql = $"UPDATE AspNetUserRoles SET RoleId = '{model.Role}' WHERE UserId = '{model.Id}'";
dbConnection.Query<DbAspNetUserRoles>(sql).ToList();
}
}
}