There's an important step missing from the question (and the other answer): the incoming PM is appointed on the advice - or at least, the recommendation - of the outgoing PM*.
It is assumed that the outgoing PM will make a sensible choice - and they are entitled to wait until a suitable candidate emerges, as happened in 2010.
In that regard, the King is essentially unable to ignore that advice, and convention is that only the Commons can challenge the subsequent appointment, initially by voting down the subsequent King's Speech, or later via a vote of no confidence in the government.
There is precedent for a legal challenge to a PM's advice, as was used during Brexit. However, the circumstances of that challenge were quite specific, and might not apply in the appointment of a PM - especially since the aforementioned mechanisms exist to remove a PM.
* Regarding the role of the outgoing PM on the choice of their successor: it's a matter of convention, but the Cabinet Manual has this to say:
Historically, the Sovereign has made use of
reserve powers to dismiss a Prime Minister
or to make a personal choice of successor,
although this was last used in 1834 and
was regarded as having undermined
the Sovereign.
In modern times the
convention has been that the Sovereign
should not be drawn into party politics,
and if there is doubt it is the responsibility
of those involved in the political process,
and in particular the parties represented
in Parliament, to seek to determine and
communicate clearly to the Sovereign who
is best placed to be able to command the
confidence of the House of Commons.
As the Crown’s principal adviser this
responsibility falls especially on the
incumbent Prime Minister, who at the time of his or her resignation may also
be asked by the Sovereign for a
recommendation on who can best
command the confidence of the House of
Commons in his or her place.
(Cabinet Manual, 2010, section 2.9)