Montana incumbent Sen. Jon Tester (D-Mont.)(shown above left) was able to defeat Republican challenger Matt Rosendale (above right) on Wednesday following a race that remained close until the very end.

Tester was hoping to win a second term on a message of bipartisanship.

Rosendale promised to protect the state and its values against federal overreach and the political class. President Donald Trump won the state on a similar message during the presidential election of 2016.

The Associated Press called the race around noon, after the vote count stayed close between the two candidates all night long. The polls closed Tuesday evening.

Rosendale is currently the Montana state auditor and was previously the majority leader of the Montana State Senate. His campaign has focused on veterans, illegal immigration, health care, gun rights and stopping federal land transfers.

He has also accused his opponent of blindly following the will of special interests and Democratic leaders.

Related: Trump Lambasts Tester, Rallies Montana Republicans for Rosendale

Trump played a notable role in the race, having won the state by 56.4 percent in 2016. He has held dozens of rallies across the country in recent months to stir support for conservative candidates, including four in support of Rosendale. The president also admitted recently that he has a personal disdain for Tester.

The president was fully invested in the midterms overall, as the election results threatened potentially to put his agenda at risk. He has held numerous rallies, interviews and press conferences to urge voters to back candidates that would help him fulfill his agenda.

He increased those efforts in the final week — he traveled to a number of states in recent days for 11 different rallies.

Who do you think would win the Presidency?

By completing the poll, you agree to receive emails from LifeZette, occasional offers from our partners and that you've read and agree to our privacy policy and legal statement.

The Montana race remained extremely close in polling right up until voters cast their ballots. The RealClearPolitics average of recent polls on the race put Tester at a 3.3 point lead — but still ranked the race a toss-up. Scottrasmussen.com put the race slightly in favor of the Democrats toward the end after it remained a toss-up for months.

Tester presented himself as someone who is willing to rise above politics and work with the president for the benefit of the state. He even highlighted legislation he was involved in that the president has signed. He’s made issues like government accountability, veterans, education, infrastructure and jobs the centerpieces of his campaign.

Trump admitted why he has so much animosity for Tester during a recent rally in the state on October 18. Tester had accused White House physician Ronny Jackson of passing prescription drugs around the White House after he was nominated as secretary of the Department of Veterans Affairs.

The president said he would never forgive him for that accusation.

Related: Farmer and Rancher Face Off in Montana’s Key 2018 Senate Race

Rosendale and Tester also highlighted their deep connection to the state alongside their experiences in farming and ranching. Tester grew up and worked on a farm, which has been in his family for three generations. Rosendale has lived on his ranch for nearly two decades, though critics have argued his experience amounts to more of a hobby.

Tester had the advantage when it came to fundraising, at $19,227,650. He got most of those funds through large individual contributions at 58 percent. Rosendale lagged far behind those fundraising efforts, at only $4,722,943. He also received most of those funds through large individual contributions at 71 percent.

Trump’s win in the presidential election of 2016 was seen by many as a rebuke of elitist dogma that hurt everyday people.

The midterms overall have drawn a considerable amount of attention, indicative of the political unrest across the country. Trump’s win in the presidential election of 2016 was seen by many as a rebuke of elitist dogma that hurt everyday people.

But critics saw the midterms as a way to challenge the president and what they perceived to be a racist and hateful agenda.

The Center for Responsive Politics released a report last week projecting that the current midterm elections will be the most expensive ever at more than $5.2 billion. Every prior election didn’t even surpass $4.2 billion in spending when adjusted for inflation. The overall estimated cost would represent a 35 percent increase from 2014.

Check out this video: