Wednesday, June 22, 2011

Working with Macro Parameters – Razor approach

As we know we can supply values for parameters on Macros. When we insert a macro onto a template, we are prompted to supply the values for the parameters. Here I would like to show you that we can also do this in a Razor script as well.

Let’s say we have a Razor script file called “WelcomeMessage.cshtml” created under the Scripting files and we have a corresponding macro called “Welcome Message Macro”. The duty of our Razor script here is to display a Welcome message. Well, we have set a macro parameter called “AttendeeName” which accepts a name when inserted on a template. Now we need to modify our welcome message to display as “Welcome [AttendeeName], have a nice time here!!!”. Here in the given message [AttendeeName] should be replaced by the value assigned as a macro parameter.

This can be easily implemented in Razor script. In order to implement this, we need to make use of the following syntax in Razor scripts as follows

@Parameter.AliasName (please note this is not case sensitive)

So in our scenario, the whole Razor script would be as given below

Welcome @Parameter.AttendeeName, have a nice time here !!!

So guys have fun with Razor :-)

No comments:

Post a Comment

Installing ASP.NET MVC

Installing ASP.NET MVC Before we dive deep into the ASP.NET MVC, let’s install it our development machine. This is very straight forwa...