The trial court did not err in awarding attorney fees to defendant noteholder when it prevailed against plaintiff borrower’s negligence and fraud claims that were aimed at preventing enforcement of the note and deed of trust through nonjudicial foreclosure. Even though the plaintiff pursued tort theories, the gist of the action was to prevent enforcement of the note and deed of trust and thus it was an action on a contract within Civ. Code 1717’s meaning. Moreover, the note and deed of trust provided that the noteholder was entitled to recover its expenses, including attorney fees, in enforcing the note or exercising remedies under the deed of trust, and that language was broad enough to allow recovery of attorney fees incurred in defending against tort claims that sought to bar enforcement of the note or foreclosure under the deed of trust.