The first port of call if you've dislocated your knee is to visit a healthcare professional so it can be relocated. If they're unable to relocate it, you should follow their instructions or go to an emergency department for assistance.
Instability of the kneecap is a highly correctable problem if it's treated early. An MRI is the best way to see whether there's an issue with the knee and if it can be fixed surgically. Depending on the cause of the instability, open surgery, keyhole surgery or a reconstruction might be required.