我有以下基本结构的MVC的用户控件:
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:
编译器错误信息:CS0452:本 类型'小数'必须是引用 为了型使用它作为参数 的TModel在泛型类型或方法 System.Web.Mvc.ViewUserControl
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?
推荐答案我会建议你一个ViewModel内包装的值类型。这将允许在未来某些灵活性(其可以或可以不需要)。
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) %>更多推荐
在使用强类型MVC视图用户控件的值类型
发布评论