在使用强类型MVC视图用户控件的值类型

编程入门 行业动态 更新时间:2024-10-27 15:27:29
本文介绍了在使用强类型MVC视图用户控件的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下基本结构的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视图用户控件的值类型

本文发布于:2023-11-16 00:01:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   视图   控件   用户   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!