Asp.net mvc 能否创建int或enum类型的强类型ASP.NET MVC ViewUserControl?

Asp.net mvc 能否创建int或enum类型的强类型ASP.NET MVC ViewUserControl?,asp.net-mvc,viewusercontrol,Asp.net Mvc,Viewusercontrol,我希望创建一个可重用的ASP.NET MVC ViewUserControl,它是强类型的枚举 这能做到吗?当我尝试时,它说ViewUserControl可以接受的强类型只能是引用类型:( 这也意味着我不能将int作为TModel传递 为什么我要这样做?在我网站的各个地方,我显示了一个依赖于枚举的简单图像。因此,我不希望在多个地方复制该逻辑,而是希望有这个可恢复的ViewUserControl并传入枚举 例如 干杯:)好吧,你可以: public sealed class Box<T>

我希望创建一个可重用的ASP.NET MVC ViewUserControl,它是强类型的枚举

这能做到吗?当我尝试时,它说ViewUserControl可以接受的强类型只能是引用类型:(

这也意味着我不能将int作为TModel传递

为什么我要这样做?在我网站的各个地方,我显示了一个依赖于枚举的简单图像。因此,我不希望在多个地方复制该逻辑,而是希望有这个可恢复的ViewUserControl并传入枚举

例如

干杯:)

好吧,你可以:

public sealed class Box<T> where T : struct {
    public Box(T value) { Value = value; }
    public T Value { get; private set; }
    public static explicit operator T(Box<T> item) {
        return item.Value; } // also check for null and throw an error...
    public static implicit operator Box<T>(T value) {
        return new Box<T>(value); }
}
公共密封类框,其中T:struct{
公用框(T值){value=value;}
公共T值{get;私有集;}
公共静态显式运算符T(框项){
return item.Value;}//还要检查null并抛出错误。。。
公共静态隐式运算符框(T值){
返回新框(值);}
}

使用
Box
Box
,等等-但就我个人而言,我希望使用非类型化视图和简单强制转换会更容易。

re:untyped view-相同。那就是我要去的地方。忘了做一个struct:P好主意,如果我想让它保持强类型的话。再次感谢马克:)
<% Html.RenderPartial("AnimalFileImageControl", animalType); %>
AnimalType animalType = (AnimalType) ViewData.Model;
    switch (animalType)
    { ... etc ... }
public sealed class Box<T> where T : struct {
    public Box(T value) { Value = value; }
    public T Value { get; private set; }
    public static explicit operator T(Box<T> item) {
        return item.Value; } // also check for null and throw an error...
    public static implicit operator Box<T>(T value) {
        return new Box<T>(value); }
}