canimbenim
canimbenim

Reputation: 649

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 e.g. PHP. I did:

  1. Created 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,

  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

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?

Upvotes: 2

Views: 657

Answers (1)

SLaks
SLaks

Reputation: 887195

Remove the ; from the call to the helper.

<%: ... %> hold expressions, not statements, and should not have semicolons.

Upvotes: 5

Related Questions