Detergent dispenser lid will not stay closed

The lid to the detergent dispenser has always been difficult to close but lately it will not stay closed at all. I have to start the dishwasher, then open the door and add detergent at the appropriate time.

I noticed that there are several parts that control the lid and I don’t know whether to replace the whole dispenser assembly or some of the individual parts.
Help!

The lid is fairly easy to replace,
but costs approx $25,
while the whole dispenser is about $40
and is a more certain fix.

The lid is removed by gently prying one side free of the rail,
then prying the spring loop from the plastic post.

Dispenser removal and replacement requires outer door panel
and is considerably more of a bother.