我试图做一个声明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动态< DIV> &所述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
发布评论