The trial court properly awarded Travelers’ opponent attorney fees under CC 1717. The opponent had successfully prosecuted an administrative complaint which resulted in voiding the agreement containing the attorney fee clause. The administrative proceeding was an “action” for purposes of CC 1717 and it was “on the contract” since had Travelers prevailed it could have collected attorney fees for enforcing the agreement.