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?
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) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With