views:

462

answers:

2

Hello,
I am trying to use a nullable datetime and double as a parameter for an actionfilter but it's gives the following error:

'Propertyname' is not a valid named attribute argument because it is not a valid attribute parameter type

I thought a quick google would solve it but to my surpise I couldn't find a lot of info about it.

Here is the code of my filter.

public class AddToSitemap : ActionFilterAttribute
{
    public string Changefreq { get; set; }
    public DateTime? Lastmod { get; set; }
    public double? Priority { get; set; }
}

Thanks in advance.

A: 

I think you need to use a string instead and convert that to DateTime in your action method or ViewModel, etc.

Adrian Grigore
+2  A: 

Here is the compiler error on the MSDN.

You can only use following types according to Attributes Tutorial:

Attribute parameters are restricted to constant values of the following types:

- Simple types (bool, byte, char, short, int, long, float, and double)
- string
- System.Type
- enums
- object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
* One-dimensional arrays of any of the above types

Which means you in your case that cannot use:

  1. Nullable Double.
  2. Nullable DateTime.
  3. DateTime.

I would advice to use int or enumeration for the Priority as priority doesn't sound to be a good candidate for beeing Double.

You can workaround DateTime by changing its type to long and assigning Ticks to it.
But I doubt that C# allows you to assign non-constant values for attributes when applying them (and new DateTime(1234,5,6).Ticks is not a constant from that perspective).

Dmytrii Nagirniak
excellent answer!
Keith Fitzgerald