Coding is a highly valued skill used in many disciplines across the globe. However, it is reported that students’ coding interest and self-efficacy are usually low. Theoretically, the affective domain of coding is in its infancy, and very few studies have set out to identify the determinants of coding interest and self-efficacy. This study explored how mathematics interest and self-efficacy, as two important mathematics-related factors, influenced coding interest and self-efficacy. Employing a quantitative cross-sectional survey design, 605 students from two public senior high schools in two big cities in China were investigated. A two-step structural equation modeling approach was adopted to analyze the data. Findings showed that mathematics interest, directly and indirectly, influenced coding interest significantly. In addition, coding self-efficacy was indirectly impacted by mathematics interest and coding interest was indirectly impacted by mathematics self-efficacy significantly. Most importantly, our model has proved to be powerful in explaining coding interest. This study contributes to the theoretical and practical understanding of the relationship between mathematics and coding attitudes, seldom explored in senior high school settings.