简体   繁体   English

MVC剃刀视图中的整数扩展

[英]Integer extension in mvc razor view

I am all new with ASP.NET MVC and Extension methods. 我是ASP.NET MVC和扩展方法的新手。

I have created two Extensions that i want to use in my View: 我创建了两个要在视图中使用的扩展:

public static class Extensions
{
    public static string ToYesNo(this bool value)
    {
        return value ? "Yes" : "No";
    }

    public static string MonthToString(this int value)
    {
        return (value >= 1 && value <= 12) ? CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(value) : "";
    }
}

I can use ToYesNo with a bool in the View, but i cannot view MonthToString with an integer. 我可以在视图中将ToYesNo与bool一起使用,但是无法使用整数查看MonthToString。 I get: 我得到:

'int' does not contain a definition for 'MonthToString'

The Extensions are in a namespace called BitvaerkAdmin.Models, and i reference that in th cshtml file. 这些扩展位于名为BitvaerkAdmin.Models的命名空间中,我在cshtml文件中对其进行引用。

Why can't i use my integer extension? 为什么我不能使用整数扩展名?

Edit: 编辑:

I reference the extensions in my view like this: 我在我的视图中引用扩展名是这样的:

@using BitvaerkAdmin.Models


<h3>
    @ViewBag.Month.MonthToString()
</h3>



@foreach (Order order in ViewBag.Orders)
{
    <td>
    @order.Valid.ToYesNo()
    </td>
}

OK, now that you have shown your code it is clear why it doesn't work. 好的,既然您已经显示了代码,那么很清楚为什么它不起作用。 You use ViewBag ( the root of all evil in ASP.NET MVC and the origin of all problems that people are having - little addition from the author of this answer). 您使用ViewBagASP.NET MVC中万恶的根源,以及人们所遇到的所有问题的根源 -这个答案的作者几乎没有添加)。

Once you borrow its path the fall to the abyss is eminent. 一旦您借用了它的道路,就深深陷入了深渊。 This fall will be accelerated by the cast that you need to perform in order to make it work: 为了使其正常工作,您需要执行的强制转换会加速这种下降:

@((int)(ViewBag.Month).MonthToString())

Simply try running the following console application and you will understand that dynamic variables cannot be used to dispatch extension methods: 只需尝试运行以下控制台应用程序,您将了解动态变量不能用于调度扩展方法:

public static class Extensions
{
    public static string MonthToString(this int value)
    {
        return (value >= 1 && value <= 12) ? CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(value) : "";
    }
}

class Program
{
    static void Main()
    {
        dynamic foo = 123;
        Console.WriteLine(foo.MonthToString()); // crash at runtime
    }
}

See why I always critique ViewBag when I see people using it? 看到为什么看到人们使用ViewBag时我总是批评它吗? Because it leads you to all kind of strange things. 因为它会导致您遇到各种奇怪的事情。 You lose Intellisense, you cannot dispatch extension methods on dynamic variables, ... 您将失去Intellisense,无法在动态变量上调度扩展方法,...

So actually you don't need to cast. 所以实际上您不需要强制转换。 You shouldn't use any ViewBag/ViewData at all. 您根本不应使用任何ViewBag / ViewData。 You should be using strongly typed view models: 您应该使用强类型视图模型:

@using BitvaerkAdmin.Models
@model MyViewModel

<h3>
    @Model.Month.MonthToString()
</h3>

@foreach (Order order in Model.Orders)
{
    <td>
        @order.Valid.ToYesNo()
    </td>
}

and to avoid the foreach loop you could use display templates: 为了避免foreach循环,您可以使用显示模板:

@using BitvaerkAdmin.Models
@model MyViewModel

<h3>
    @Model.Month.MonthToString()
</h3>

@Html.DisplayFor(x => x.Orders)

and then define a display template for the order which will automatically be rendered by the framework for all elements of the collection ( ~/Views/Shared/DisplayTemplates/Order.cshtml ): 然后为订单定义一个显示模板,该模板将由框架自动为集合的所有元素呈现( ~/Views/Shared/DisplayTemplates/Order.cshtml ):

@using BitvaerkAdmin.Models
@model Order
<td>
    @Model.Valid.ToYesNo()
</td>

Everything is now strongly typed and working. 现在,所有内容都经过严格键入并可以正常工作。

After giving reference of Extention class in My view I tried as below and it worked for me. 在我的观点中给出了Extension类的参考之后,我尝试了以下方法,它对我有用。

    @using NameSpace Of your Extentions class;
    @{
      int i = 10;          
    }
    <span>@i.MonthToString()</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM