My favorites | Sign in
Project Logo
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel;
using System.Globalization;

namespace ManagedFusion.Web.Mvc
{
/// <summary>
///
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public class DefaultAttribute : CustomModelBinderAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultAttribute"/> class.
/// </summary>
/// <param name="default">The @default.</param>
public DefaultAttribute(object @default)
{
if (@default == null)
throw new ArgumentNullException("default");

Default = @default;
}

/// <summary>
/// Gets or sets the default.
/// </summary>
/// <value>The default.</value>
public object Default
{
get;
private set;
}

/// <summary>
/// Gets the binder.
/// </summary>
/// <returns></returns>
public override IModelBinder GetBinder()
{
return new DefaultValueModelBinder(Default);
}

/// <summary>
///
/// </summary>
private class DefaultValueModelBinder : DefaultModelBinder
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultAttribute"/> class.
/// </summary>
/// <param name="default">The @default.</param>
public DefaultValueModelBinder(object @default)
{
if (@default == null)
throw new ArgumentNullException("default");

Default = @default;
}

/// <summary>
/// Gets or sets the default.
/// </summary>
/// <value>The default.</value>
public object Default
{
get;
private set;
}

/// <summary>
/// Converts the type.
/// </summary>
/// <param name="culture">The culture.</param>
/// <param name="value">The value.</param>
/// <param name="destinationType">Type of the destination.</param>
/// <returns></returns>
protected override object ConvertType(System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value != null)
return base.ConvertType(culture, value, destinationType);

// try to convert the default to the type of the output
return base.ConvertType(culture, Default, destinationType);
}
}
}
}
Show details Hide details

Change log

r25 by nberardi on Sep 18, 2008   Diff
update to latest version of ManagedFusion
Go to: 
Project members, sign in to write a code review

Older revisions

r24 by nberardi on Aug 13, 2008   Diff
added some new features, and removed a
couple provided by the default install
of the MVC framework
All revisions of this file

File info

Size: 2312 bytes, 92 lines
Hosted by Google Code