Html Helpers

Html helpers are like functions/methods, they are pretty easy to create and very useful.

Helpers can be created in a complete different view called “HtmlHelpers.cshtml” for example. But for the sake of this post ill add it to my ¬†current view.

<h1>Helper view</h1>
@helper MyCustomHelper(string name, string place) {
<div>
-------------------------------------------****------------------------------------------
Hi <span style="background-color:#dddbdb">@name</span> !
Welcome to : <span style="background-color:#dddbdb">@place</span>
-------------------------------------------****------------------------------------------</div>
}

@MyCustomHelper(name:"Dev" , place: "public string notes")

“MyCustomHelper” its pretty self explanatory, it has 2 string which can be used witht the usual “@”.

The result:

helperView

Now if you need to clone this block, just call the method and send another string as param, no need to write more code. Keep it DRY.

 

Advertisements

The “UIHint” Decorator

Today’s discover was a handy csharp decorator, the “UIHint”.

uihtin

The string is the name of the template that we want to call.

Next in our view we call the displayFor and we pass the Color property.

view

Looking good, but we still need to call our template. So we create a folder called “DisplayTemplates” and inside we add a new View with the same name as the string in our decorator, in this case “ColorTemplate”.

folders

And finally inside our template view we just need to say what we want.

for example ,use the properties of our model to pass as arguments to the style of our div.

view2

 

End