Fix operator != handling of LHS null
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ByteArrayModelBinder.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15 \r
16     public class ByteArrayModelBinder : IModelBinder {\r
17         public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {\r
18             if (bindingContext == null) {\r
19                 throw new ArgumentNullException("bindingContext");\r
20             }\r
21 \r
22             ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);\r
23 \r
24             // case 1: there was no <input ... /> element containing this data\r
25             if (valueResult == null) {\r
26                 return null;\r
27             }\r
28 \r
29             string value = valueResult.AttemptedValue;\r
30 \r
31             // case 2: there was an <input ... /> element but it was left blank\r
32             if (String.IsNullOrEmpty(value)) {\r
33                 return null;\r
34             }\r
35 \r
36             // Future proofing. If the byte array is actually an instance of System.Data.Linq.Binary\r
37             // then we need to remove these quotes put in place by the ToString() method.\r
38             string realValue = value.Replace("\"", String.Empty);\r
39             return Convert.FromBase64String(realValue);\r
40         }\r
41     }\r
42 \r
43 }\r