Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.ComponentModel;
11 : using System.Web.Mvc;
12 :
13 : namespace Cqrs.Web.Mvc
14 : {
15 : /// <summary>
16 : /// A <see cref="IModelBinder"/> designed to return <see cref="Guid.Empty"/> is not <see cref="Guid"/> is provided.
17 : /// </summary>
18 : public class NullableGuidBinder : DefaultModelBinder
19 1 : {
20 : /// <summary>
21 : /// Returns the value of a property using the specified <paramref name="controllerContext"/>, <paramref name="bindingContext"/>, <paramref name="propertyDescriptor"/>, and <paramref name="propertyBinder"/>.
22 : /// </summary>
23 : /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
24 : /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
25 : /// <param name="propertyDescriptor">The descriptor for the property to access. The descriptor provides information such as the component type, property type, and property value. It also provides methods to get or set the property value.</param>
26 : /// <param name="propertyBinder">An object that provides a way to bind the property.</param>
27 : /// <returns>An object that represents the property value.</returns>
28 1 : protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
29 : {
30 : if (propertyDescriptor.PropertyType == typeof(Guid))
31 : {
32 : object rawValue = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
33 : if (rawValue == null)
34 : return Guid.Empty;
35 : return rawValue;
36 : }
37 : return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
38 : }
39 : }
40 : }
|