简体   繁体   English

ASP.NET MVC2 自定义日期时间格式助手

[英]ASP.NET MVC2 custom dateTime format helper

I have a question on how to create custom View Helpers.我有一个关于如何创建自定义视图助手的问题。 I would like, in my Index View, to change my default dateTime format.我想在我的索引视图中更改我的默认日期时间格式。 I want to write my own Helper as I thought it was easy like in eg PHP.我想编写自己的 Helper,因为我认为这很容易,例如 PHP。 I did:我做了:

  1. Created Class:创建 Class:

     public static class CustomViewHelpers { public static MvcHtmlString returnDateString(string format, DateTime date) { return MvcHtmlString.Create(date.ToString(format)); } }
  2. I added it to the web.config,我将它添加到 web.config 中,

  3. I used it like that:我是这样使用它的:

     <p><em> Birth date</em> <%: CustomViewHelpers.returnDateString("D", Model.Birth_Date); %></p>

But I always get the error:但我总是得到错误:

Compilation Error编译错误

Description: An error occurred during the compilation of a resource required to service this request.说明:在编译服务此请求所需的资源期间发生错误。 Please review the following specific error details and modify your source code appropriately.请查看以下特定错误详细信息并适当修改您的源代码。

Compiler Error Message: CS1026: ) expected编译器错误消息:CS1026:) 预期

But I think it is a wrong error because when I remove my line it works correctly and I see there are these ")" marks.但我认为这是一个错误的错误,因为当我删除我的行时它可以正常工作并且我看到有这些“)”标记。 Can you explain to me why this kind of helper does not work?你能解释一下为什么这种助手不起作用吗? Maybe you know a good tutorial on how to write your own helpers in ASP.NET MVC2?也许你知道一个关于如何在 ASP.NET MVC2 中编写自己的助手的好教程?

Remove the ;删除; from the call to the helper.从呼叫帮助者。

<%: ... %> hold expressions , not statements, and should not have semicolons. <%: ... %>包含表达式,而不是语句,并且不应该有分号。

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

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