tags:

views:

893

answers:

6

I've got an MVC user control with the following basic structure:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Decimal>" %>

<%= Math.Round(Model) %>

Which gives this error message when I use it:

Compiler Error Message: CS0452: The type 'decimal' must be a reference type in order to use it as parameter 'TModel' in the generic type or method 'System.Web.Mvc.ViewUserControl'

Is there a way to get this to work (somehow tricking the framework into treating the Decimal as a reference type maybe?) Or is what I'm trying to do just fundamentally wrong?

+3  A: 

ViewUserControl is declared as follows:

public class ViewUserControl<TModel> : ViewUserControl
where TModel : class

So you can't trick it into compiling with a value type. I'd recommend wrapping your Decimal object in another class.

Johnny G
A: 

I'm not terribly familiar with the ASP.Net MVC Framework, but the error is saying your model has to be a class, not a primitive type, which is pretty standard from a model view controller perspective.

To get this to work, you'd need a class like:

class MyDouble : Model   // or whatever MVC calls it's Model
{
   public TheValue {
     get;
     set;
   }
}

and then replace your line with <%= Math.Round(Model.TheValue) %>.

Note, It seems like a strange design to have a custom control for just a double value. If this is just a sample app, then fine, but otherwise, you may want to reconsider how to best use custom controls.

Tim Hoolihan
I am sorry but your example will not compile since the property "TheValue" has **NO** type.
Andrei Rinea
+2  A: 

You can wrap the value in a class:

 public class Wrapper
{
        public Wrapper(decimal d)
        {
            this.Value = d;   
        }
        Decimal Value { get; set; }
}

In the View:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Wrapper>" %>
<%= Math.Round(Model.Value) %>
Marwan Aouida
+7  A: 

I would suggest you wrap the value type within a ViewModel. This will allow for some flexibility in the future (which may or may not be needed).

public class MyUserControlViewModel
{
    public Decimal MyValue { get; private set; }

    public MyUserControlViewModel(Decimal dec)
    {
        MyValue = dec;
    }
}


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyUserControlViewModel>" %>

<%= Math.Round(Model.MyValue) %>
Jon Erickson
+1  A: 

The model is a DTO from the controller to the view. You should create your own DTO even with a single decimal property. The view's responsibility is only about rendering, the Math.Round should be executed by the controller.

Andrea Balducci
+1  A: 

While I strongly support all the answers above, stating to wrap the decimal value in a class to support further development you can quickly solve your problem by making the decimal nullable ("decimal?") since System.Nullable is a reference type.

Andrei Rinea
Actually, this doesn't work either. I get the same error but with "Decimal?" instead of "Decimal"
kristian