500 indicates a server side error so the problem is in your C# code. This is where standard debugging comes into play as we have no way of debugging your really, really long query to figure out what is wrong.
- Set a breakpoint at top of your action method.
- Start the debugger.
- Navigate in your app until the action method is called.
- Step through your code.
- When exception occurs the debugger will tell you exactly what is wrong and where it failed.
- Provide all that information so somebody can help you if you are unable to figure it out on your own.