我应该在哪里放置声明HTML佣工在ASP.NET MVC 3

编程入门 行业动态 更新时间:2024-10-18 12:25:56
本文介绍了我应该在哪里放置声明HTML佣工在ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图做一个声明HTML帮助中 ScottGu的剃刀规定帖子,但我没有多少运气。我试图把一个Helpers.cshtml文件与查看/佣工DateTimeHelper但它不会把它捡起来,所以我在APP_ code试图按the MVC 3 Beta版后。现在,它把它捡起,但是当我尝试使用它在一个文件中像这样:

@DateTimeHelper(DateTime.Now)

编译器抱怨DateTimeHelper不存在。

一些奇怪的事情:如果我重命名文件DateTime.cshtml,我得到一个不同的错误,一些关于特定code在我的帮手。

有关完整起见,这里是辅助的code:

@helper DateTimeHelper(日期时间T,布尔的longdate =真,布尔=的showTime真,布尔长期=真){    <时间日期时间=@ t.ToUniversalTime()'>        @if(的longdate){            如果(的showTime){                如果(长期){                    @ t.ToLongDateString()@ t.ToLongTimeString();                }其他{                    @ t.ToLongDateString()@ t.ToShortTimeString();                }            }其他{                @ t.ToLongDateString()            }        }其他{            如果(的showTime){                如果(长期){                    @ t.ToShortDateString()@ t.ToLongTimeString();                }其他{                    @ t.ToShortDateString()@ t.ToShortTimeString();                }            }其他{                @ t.ToShortDateString()            }        }    < /时间>}

助手在这样的特定视图中使用:

@model动态< D​​IV>    &所述p为H.;当前时间是@DateTimeHelper(DateTime.Now)LT; / P>< / DIV>

解决方案

要使用剃刀的@helper功能,您需要将CSHTML文件放置在您的应用程序的APP_ code文件夹中。有一个在ASP.NET MVC 3 ScottGu的博客文章没有查看/助手的文件夹被写入之前的功能已全面实施,而一些票据也有不完全准确了。

要调用@helper你写,你必须包括文件名,以及它里面的助手的名字。例如,如果你有这样的帮手:

〜/ APP_ code / MyHelper.cshtml

和此内容:

@helper ShowStuff(字符串的东西){    &所述p为H.; @东西&下; / P>}

然后调用它像这样:

@ MyHelper.ShowStuff(一些东西!)

I'm trying to make a declarative HTML Helper as specified in ScottGu's Razor post, but I'm not having much luck. I tried putting a Helpers.cshtml file with a DateTimeHelper in Views/Helpers but it wouldn't pick it up, so I tried in App_Code as per the MVC 3 Beta post. Now it picks it up, but when I try to use it in a file like so:

@DateTimeHelper(DateTime.Now)

The compiler complains that DateTimeHelper doesn't exist.

Some weird things: If I rename the file to DateTime.cshtml, I get a different error, something about the particular code in my helper.

For completeness' sake, here's the helper's code:

@helper DateTimeHelper(DateTime t, bool longDate = true, bool showTime = true, bool longTime = true) { <time datetime='@t.ToUniversalTime()'> @if(longDate) { if(showTime) { if(longTime) { @t.ToLongDateString() @t.ToLongTimeString(); } else { @t.ToLongDateString() @t.ToShortTimeString(); } } else { @t.ToLongDateString() } } else { if(showTime) { if(longTime) { @t.ToShortDateString() @t.ToLongTimeString(); } else { @t.ToShortDateString() @t.ToShortTimeString(); } } else { @t.ToShortDateString() } } </time> }

The helper is used in a certain view like this:

@model dynamic <div> <p> The current time is @DateTimeHelper(DateTime.Now)</p> </div>

解决方案

To use the "@helper" feature in Razor you need to place the CSHTML file in the App_Code folder of your app. There is no "Views/Helpers" folder in ASP.NET MVC 3. ScottGu's blog post was written before the feature was fully implemented, and some of the notes there are not entirely accurate anymore.

To call the "@helper" that you wrote you have to include both the filename as well as the name of the helper inside it. For example, if you have this helper:

~/App_Code/MyHelper.cshtml

And this content:

@helper ShowStuff(string stuff) { <p>@stuff</p> }

Then you call it like so:

@MyHelper.ShowStuff("some stuff!")

更多推荐

我应该在哪里放置声明HTML佣工在ASP.NET MVC 3

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

发布评论

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

>www.elefans.com

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