Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I place Declarative HTML helpers in ASP.NET MVC 3

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>
like image 780
Diego Avatar asked Oct 20 '10 18:10

Diego


People also ask

Where do we use HTML helpers?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.

How can create HTML helper in ASP.NET MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What is the role of HTML helper in ASP.NET MVC?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.


1 Answers

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!")
like image 95
Eilon Avatar answered Oct 04 '22 18:10

Eilon