Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Model-binding is being case-sensitive when binding Url data to Enum parameter #913

Closed
kichalla opened this issue Aug 1, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@kichalla
Copy link
Member

kichalla commented Aug 1, 2014

For the following action, sending a request like this fails model validation:

Succeeds:
products/search?categories=Books&searchterm=adidas

Fails((note the case of books):
products/search?categories=books&searchterm=adidas

Action:

public ActionResult Search(List<ProductCategoryType> categories, string searchTerm)

Enum:

public enum ProductCategoryType
    {
        Books,
        Movies,
        Sports,
        Clothing
    }

Error:

{  
   "categories":{  
      "Value":{  
         "AttemptedValue":"books",
         "Culture":"(Default)",
         "RawValue":"books"
      },
      "Errors":[  
         {  
            "Exception":{  
               "ClassName":"System.ArgumentException",
               "Message":"Requested value 'books' was not found.",
               "Data":null,
               "InnerException":null,
               "HelpURL":null,
               "StackTraceString":"   at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)\r\n   at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)\r\n   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)\r\n   at System.Enum.Parse(Type enumType, String value)\r\n   at Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult.<>c__DisplayClass0.<GetConverterDelegate>b__20(Object value, CultureInfo culture) in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\ValueProviders\\ValueProviderResult.cs:line 241\r\n   at Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType) in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\ValueProviders\\ValueProviderResult.cs:line 98\r\n   at Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType) in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\ValueProviders\\ValueProviderResult.cs:line 144\r\n   at Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult.ConvertTo(Type type, CultureInfo culture) in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\ValueProviders\\ValueProviderResult.cs:line 66\r\n   at Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult.ConvertTo(Type type) in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\ValueProviders\\ValueProviderResult.cs:line 47\r\n   at Microsoft.AspNet.Mvc.ModelBinding.TypeConverterModelBinder.<BindModelAsync>d__1.MoveNext() in C:\\ProjectK\\MVC\\src\\Microsoft.AspNet.Mvc.ModelBinding\\Binders\\TypeConverterModelBinder.cs:line 33",
               "RemoteStackTraceString":null,
               "RemoteStackIndex":0,
               "ExceptionMethod":"8\nSetFailure\nmscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Enum+EnumResult\nVoid SetFailure(ParseFailureKind, System.String, System.Object)",
               "HResult":-2147024809,
               "Source":"mscorlib",
               "WatsonBuckets":null,
               "ParamName":null
            },
            "ErrorMessage":""
         }
      ],
      "ValidationState":"Invalid"
   },
   "searchTerm":{  
      "Value":{  
         "AttemptedValue":"adidas",
         "Culture":"(Default)",
         "RawValue":"adidas"
      },
      "Errors":[  

      ],
      "ValidationState":"Valid"
   }
}
@kichalla kichalla changed the title Model-binding is being case-sensitive when binding data from url Model-binding is being case-sensitive when binding Url data to Enum parameter Aug 1, 2014
@pranavkm
Copy link
Contributor

pranavkm commented Aug 1, 2014

Seems like a specific issue with the Enum conversion - https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/ValueProviderResult.cs#L241 needs to use the ignoreCase overload.

@yishaigalatzer yishaigalatzer added this to the 6.0.0-alpha4 milestone Aug 1, 2014
@danroth27
Copy link
Member

Likely to be resolved with TypeConverter support.

@danroth27 danroth27 modified the milestones: 6.0.0-alpha4, 6.0.0-beta1 Sep 24, 2014
sornaks pushed a commit that referenced this issue Oct 12, 2014
…ata to Enum parameter.

Fix: Using TypeConverter solves this problem.
Issue #1123 - TypeConverterModelBinder cannot bind "byte" and "short".
Fix: Modified code to use TypeConverter which can handle these scenarios.
sornaks pushed a commit that referenced this issue Oct 14, 2014
…ata to Enum parameter.

Fix: Using TypeConverter solves this problem.
Issue #1123 - TypeConverterModelBinder cannot bind "byte" and "short".
Fix: Modified code to use TypeConverter which can handle these scenarios.
sornaks pushed a commit that referenced this issue Oct 14, 2014
…data to Enum parameter.

Fix: Using TypeConverter solves this problem.
-Issue #1123 - TypeConverterModelBinder cannot bind "byte" and "short".
Fix: Modified code to use TypeConverter which can handle these scenarios.
-Removing the GetConverterDelegate method and making the code similar to the WebApi.
@sornaks
Copy link

sornaks commented Oct 14, 2014

Checked in - 5fa8a91

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants