MVC : using Json pass enum value
What's if i pass value from client to server as Json post, and server side's paramter would be enum. This's article is about that. Basically, you should use Nullable, otherwise, it'll throw exception by .NET.
1. Your enum
2. Controller(failed case)
If you define a parameter like this, it'll throw exception when you call it.
3. Sample HTML
When you click button without enter anything, which is empty, it'll throw exception.
Basically, add ? to your enum. Then, if you passing is not enum int value, it'll pass as null to server side.
7. Compare result
So, basically, you could use ? if you passing enum to your controller. Since .net convert client value to enum works fine so far, it's better to use Nnum?, then you don't need to convert from string or int to your enum again at your C# code.
- C# add extension to all enum
- MVC : using Json pass enum value
- Using C# Extension for enum, int, bool, etc
- How to validate radio button list by using Microsoft MVC - ModelState Validation
- Build your own simple logging utility using C#
- Using C# default keyword
- MVC : using Json pass View Model
- C# : How to use object Initializers
- Sample Code for sending Firebase Cloud Message (FCM) with Xamarin
- Unit Test in C#