Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:44:15+00:00 2026-05-22T18:44:15+00:00

I have a method in my controller that accepts an object as an argument

  • 0

I have a method in my controller that accepts an object as an argument and returns a JsonResult. One of the properties on this object is an enum with three possible values. I assumed that when the client passed in an int for that property it would populate the enum, but it doesn’t, it defaults to 0 and the enum is set to the first of it’s possible selections.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:44:16+00:00Added an answer on May 22, 2026 at 6:44 pm

    NOTE: This has been resolved in MVC 4. If upgrading to MVC 4 is a viable option for your project then that is all you have to do in order to begin model-binding to enums.

    That said, here is the workaround for MVC 3 if you still need it.


    The issue is with the default model binder in MVC. The correct integer value makes it to the model binder but the binder is not coded to map to the integer value of the enum. It correctly binds if the value being passed in is a string containing the named value of the enum. The problem with this is that when you parse a C# object into JSON using the Json() method it sends the integer value as the enum value, not the named value.

    The easiest and most transparent fix for this is to override the default model binder and write some custom logic to fix the way it binds enums.

    1. Create a new class, like so.

      namespace CustomModelBinders
      {
          /// <summary>
          /// Override for DefaultModelBinder in order to implement fixes to its behavior.
          /// This model binder inherits from the default model binder. All this does is override the default one,
          /// check if the property is an enum, if so then use custom binding logic to correctly map the enum. If not,
          /// we simply invoke the base model binder (DefaultModelBinder) and let it continue binding as normal.
          /// </summary>
          public class EnumModelBinder : DefaultModelBinder
          {
              /// <summary>
              /// Fix for the default model binder's failure to decode enum types when binding to JSON.
              /// </summary>
              protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
                  PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
              {
                  var propertyType = propertyDescriptor.PropertyType;
                  if (propertyType.IsEnum)
                  {
                      var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                      if (null != providerValue)
                      {
                          var value = providerValue.RawValue;
                          if (null != value)
                          {
                              var valueType = value.GetType();
                              if (!valueType.IsEnum)
                              {
                                  return Enum.ToObject(propertyType, value);
                              }
                          }
                      }
                  }
                  return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
              }
          }
      }
      
    2. Then simply register it in your Global.asax file.

      protected override void OnApplicationStarted()
      {
          base.OnApplicationStarted();
      
          AreaRegistration.RegisterAllAreas();
          RegisterRoutes(RouteTable.Routes);
      
          // Register your new model binder
          ModelBinders.Binders.DefaultBinder = new EnumModelBinder();
      }
      

    That’s it. Enums will now be correctly bound on JSON objects.

    http://www.codetunnel.com/how-to-bind-to-enums-on-json-objects-in-aspnet-mvc-3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.Net MVC if I have a method on a controller that returns a
I have class method that returns a list of employees that I can iterate
I have a method in my Python code that returns a tuple - a
I have a method that can return either a single object or a collection
I have a method defined in my controller that I am trying to create
I have controller method in my MVC application that I am calling using the
I have a controller that returns a list of files in a directory using
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
I have a controller with an action method as follows: public class InventoryController :
Say I have a controller with an Index Method and a Update Method. After

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.