Dissecting Razor, part 9: Inline Helpers

In addition to normal and static helpers, Razor supports inline helpers, (also known as templates), which allow you to create lambda expressions that return markup.

An inline helper is created by writing @<tag>Content</tag> as an expression.  This creates a lambda expression that takes a parameter called item and returns a HelperResult object containing the markup.

Inline helpers are used to create functions that take markup as parameters.  For example, you might make an IfLoggedOn helper that displays content if there is a logged-in user, but shows a login link to anonymous users.  To pass the content to the helper, you can use an inline helper:

@helper IfLoggedOn(Func<MembershipUser, HelperResult> content) {
    var currentUser = Membership.GetUser();
    if (currentUser == null) {
        <i>
            To use this content, you must 
            <a href="@Href("~/Login")">log in</a>.
        </i>
    } else {
        @content(currentUser)
    }
}

You can call this method with an inline helper like this:

@IfLoggedOn(
    @<form action="@Href("Add-Comment")" method="post">
        @Html.TextArea("Comment", "Type a comment")
        <input type="submit" name="Submit" value="Add Comment" />
    </form>
)

The @content(currentUser) call in helper method is translated by the Razor compiler into a call to the overload of the Write method that takes a HelperResult (returned from the delegate).  This overload writes the content of the HelperResult to the page without escaping it (Just like a call to a normal helper method).  Functions that take inline helpers can also get the text rendered by the helper by calling ToString() on the HelperResult.

The call to the helper method is compiled to the following C# code:

Write(IfLoggedOn(
item => new System.Web.WebPages.HelperResult(__razor_template_writer => {

    WriteLiteralTo(@__razor_template_writer, "    ");
    WriteLiteralTo(@__razor_template_writer, "<form action=\"");

    WriteTo(@__razor_template_writer, Href("Add-Comment"));

    WriteLiteralTo(@__razor_template_writer, "\" method=\"post\">\r\n        ");

    WriteTo(@__razor_template_writer, Html.TextArea("Comment", "Type a comment"));

    WriteLiteralTo(@__razor_template_writer, "\r\n   ...  </form>\r\n");

})));

The IsLoggedOn method is passed a lambda expression that takes an item parameter and returns a new HelperResult.  The HelperResult is constructed exactly like a normal helper, except that it’s in a lambda expression instead of a normal method.

Like normal helpers, then markup inside inline helpers can contain arbitrary code blocks and code nuggets.  However, inline helpers cannot be nested (since that would create conflicting parameter names)

The item parameter is implicitly typed based on the delegate type that the helper is being passed as (just like normal lambda expressions).  This allows inline helpers to accept a parameter:

@IfLoggedOn(@<text>Welcome, @item.UserName!</text>)

(The special <text> tag is used to pass markup without creating an actual HTML tag)

Next Time: Sections

63 comments:

Thank you!

Thanksa lot for this useful info

summer

Thanks for this content on the blog. Chloe

Some tips on university rankings you can check here. If you do it you will get some idea on which university to attend to

I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here regularly. I am quite certain I’ll learn plenty of new stuff right here! Good luck for the next!

office.com/setup

It is amazing post, i am really impressed of your post. Its really useful. Thank you for sharing this article.
avast login
office.com/setup
Rand McNally Update

Data security is a very big issue nowadays. Viruses harm the data. Here you can know more about how to remove viruses from your computer and makes your data secure and safe.
Easy Steps To Remove Viruses
How to Remove Viruses from your computer?

Are you looking to hire top quality Custom Term Paper Help Services and cheap Legitimate Term Paper Writing Services? Rather than risking abject failure, you should consider hiring Legitimate Term Paper Help Provider.

looking for Epson Printer drivers then epson.com/connect is best place for you. Visit to get latest & updated drivers for all Epson Printer models.

looking for help with Epson Printer issues then call Epson Printer tech Support phone number. Call us to get help & support for for all Epson Printer models.


looking for help with Malwarebytes issues then call Malwarebytes tech Support phone number . Call us to get help & support for for all Malwarebytes related issues.

looking for help with Gmail Password recovery issues then call Gmail Password recovery phone number . Call us to get help & support for for all Gmail Password related issues.

Epson has been known as one of the major brands when it comes to printers. And solve all your printer queries like Epson printer offline issue, Epson printer not printing.

Know why your Epson printer not working and get all solutions for Epson printer offline issue.

Excellent information Providing by your Article, thank you for taking the time to share with us such a nice article. Amazing insight you have on this, it's nice to find a website that details so much information about different artists. Kindly visit the LiveWebTutors website we providing the best assignment help services in Australia.

For More Info: Assignment Help Adelaide

Hire UK Essay Writers for competing Law and Other Subject Assignments from studentsassignmenthelp.com. Our writers are proficient in giving law assignment help to the students at a low cost.

Hire our Singapore Translators to complete your academic translation work.Their translation work plagiarism free,an effective,and avails in numerous pair of language.

For best thesis writing help in Auckland and Wellington visit to NZ Assignment Help. Our writers are highly professional and they are giving all the assignment help services for all local as well as international students studied in University of Auckland and University of Wellington. We have a dedicated team of 250+ assignment makers and all of them are giving 100% orignal content for your assignment help.

UAE Assignment Help is the best assignment help service provider in Sharjah and Rakcity. We are writing your content 100% written from scratch. Our highly professional team of 250+ assignment makers are giving 100% unique content for all the assignment help. We are providing our services for all the students studied in United Arab Emirates University and Dubai University.

Greetings, I’m John. I’m a writer living in London, UK. I am a fan of technology, writing. You can visit my store.
Mcafee.com/activate
Mcafee.com/activate

You need to activate your norton products?. you have to visit norton official website for download norton setup then log in your norton account with email and password. follow few steps to activate your norton setup for any help visit website.

norton.com/setup

Dragon NaturallySpeaking remains the benchmark for Windows-based speech-to-text technology.
dragon naturally speaking | dragon naturallyspeaking

McAfee Antivirus protect The basic antivirus program will do all the essential security measure www.mcafee.com/activate always protect your devices McAfee Antivirus applications which are easily available in the market and also on mcafee.com/login .McAfee antivirus offers protection to your important documents and secures your folder and files on these devices. www.mcafee.com login the most well-known antivirus for security and privacy purpose. If you wish to get McAfee antivirus on the operational system, then follow detail by detail guide by and download McAfee mcafee.com my account.
mcafee.com my account antivirus offers protection to your important documents and secures your folder and files on these devices. It's the most well-known antivirus for security and privacy purpose mcafee activate product key

McAfee service offers trusted safety to protect data and devices. So, you can shop, surf & keep all your devices safe online with the convenience of a single subscription.
www mcafee activate | mcafee login my account |
www mcafee com login
mcafee livesafe login | my mcafee account |
mcafee activation code |
mcafee.com my account

there is an expanding interest for accompanies all over the place. Thusly, this

quick increment popular has lead to bargain with quality. Kolkata Escorts

Agencies is really a name that doesn't bargain with the quality with expanding

numbers. So as to make this genuine, we have concocted a brand that gives

genuine Call Girl Services in Kolkata.
if you are intrested than you can contact: Kolkata Escorts

Microsoft Office Suite of products developed by Microsoft that includes Microsoft Word, Excel, Access, PowerPoint, and Outlook. Get some easy steps for easy downloading, installing, activating, and re-installing the Microsoft Office suite by visiting the following links.
Office.com/setup

eset internet security can be installed quickly on each device if the user follows the correct procedure. You can follow the instructions below to install the setup on your devices such as Windows PC, Mac, or mobile device. Best IT security solutions for your home and business devices. Try ESET antivirus and internet security solutions for Windows, Android, Mac or Linux OS.

Enter Norton Setup Product Key and present Norton antivirus on your PC. click Norton.com/setup Enter a New Product Key.Norton.com/setup is the great antivirus software tool that protect your system.Download Norton setup to install Norton antivirus on your computer. Activate your Norton security key for Norton setup.
Norton.com/setup

Greetings, I’m John. I’m a writer living in London, UK. I am a fan of technology, writing. You can visit my store.
Mcafee.com/activate
Norton.com/setup
Mcafee.com/activate
Mcafee.com/activate
webroot.com/safe

I recommend to write such more post like this as it will encourage others.I also recommend visiting mobile app development company

Visit McAfee Activate official site www.mcafee.com/activatein any program.

Enter your 25 digits McAfee Activation Product Key.

Sign in to your McAfee account with accreditations.

Snap Submit and Choose "Nation and language".

Once Logged in, Download your Setup in download organizer.

Run application to introduce, You're good to go to go!
for more information can visit:
mcafee.com/activate

If you suddenly face Mcafee antivirus installation error then you can instantly contact this support service team members of Mcafee and find a solution of complex issues related to Mcafee antivirus.
mcafee.com/activate

Visit McAfee official website www.mcafee.com/activate. Enter your 25 digits Mcafee activation Product Key Enter your Email and password Click Submit and Log in to your account. Once Logged in, Download your Setup in download folder. Run application to install, you’re all set to go!
mcafee.com/activate

Thanks for taking time to share with us such a great article. I found such a significant number of fascinating stuff with regards to your blog particularly its discussion. If you want to keep your Network safe from unwanted threats like viruses, Trojans, and malware, then you can simply download, install and activate the McAfee Product via McAfee.com/Activate | McAfee.com/Activate.

Greetings, I’m John. I’m a writer living in London, UK. I am a fan of technology, writing. You can visit my store.
Mcafee.com/activate
Norton.com/setup
Mcafee.com/activate
Office.com/setup
Office.com/setup

There are academic nursing writing help companies whose ultimate goal is to provide Help with Nursing Writing Services since they are aware most nursing essay writing service part-time students lack enough time to complete their college custom nursing assignments.

McAfee antivirus on their computer, at that point the significant information on their gadget gets shielded from all the infections. McAfee programming obstructs all the hurtful locales which are undependable for the clients' framework
mcafee.com/activate |
mcafee.com/activate

Norton antivirus guarantees excellent virus protection without damaging the device performance while going through the scanning process. Norton antivirus protects the system from infected sites, online browsing, viruses, Trojans, and other harmful software. It also blocks the harmful websites, which can inject code to damage all the data and files on the system. Firstly, purchase the Norton
norton.com/setup
norton setup
norton download
norton account

There are academic nursing writing help companies whose ultimate goal is to provide Help with Nursing Writing Services since they are aware most nursing essay writing service part-time students lack enough time to complete their college custom nursing assignments.

McAfee.Com/Activate – In this 21st century, the greater part the number of inhabitants on the planet utilizes the web to cause installments, to oversee financial balance applications, and numerous online business arrangements and undertakings. As each business and other work is stacking on the web, the cheats are additionally utilizing the web to take information from clients' gadgets. mcafee.com/activate | mcafee.com/activate

McAfee.Com/Activate – In this 21st century, the greater part the number of inhabitants on the planet utilizes the web to cause installments, to oversee financial balance applications, and numerous online business arrangements and undertakings. As each business and other work is stacking on the web, the cheats are additionally utilizing the web to take information from clients' gadgets.
mcafee.com/activate | mcafee activate

Social science coursework writing services are essential and it has become very popular for those seeking social science research paper writing services since most of them seek Social Science Writing Services.

Charlie Wilson is a Microsoft Office expert and has been working in the technology industry since 2002. As a technical expert, Charlie has written technical blogs, manuals, white papers, and reviews for many websites such as office.com/setup. For more information visit on office.com/setup | Norton.com/setup | office.com/setup

Protect our system from antivirus damage , from malware , from anytype of online management. Other wise our some important files can be stolen , so in order to protect visit best computer anitvirus software-
mcafee.com/activate

norton.com/setup
- Norton is one of the best antivirus software which protects your computer from various malware and viruses. It is introduced by Semantic Corp. It works in various Operating Systems like iOS, Windows and even in Android. It provides various security essentials to your system like antispyware, antivirus and antimalware. For any support and help in antivirus please contact us. click on - norton.com/setup

mcafee.com/activate
- McAfee is an antivirus that detects and eliminates harmful viruses from your system. It is one of the best two-way firewall security available for users that blocks infected sites automatically. Also, it offers several premium features that a user can grab to enjoy virtual life conveniently. For More Information Visit- mcafee.com/activate

I am a  web developer, and software engineer currently living in United States. My interests range from entrepreneurship to technology. I am having an experience of more than 6 years in field of Information Technology. i write Blog on technology and we provide service for softwares including antivirus.
richardbranson243
richardbranson243
richardbranson243
richardbranson243
richardbranson243
richardbranson243
richardbranson243
richardbranson243
richardbranson243

Charlie Wilson is a Microsoft Office expert and has been working in the technology industry since 2002. As a technical expert, Charlie has written technical blogs, manuals, white papers, and reviews for many websites such as office.com/setup. For more information visit on office.com/setup | Norton.com/setup | office.com/setup

Dear admin, first of all, thank you so much for sharing such useful information with us, I really like your writing style, you are doing such amazing work and your information is very useful for me, I have also something here to share. norton.com/setup
www.norton.com/setup, Norton product key

You could change someone's life by such an unselfish act Your content is accurate well versed with technology idea.Actually i enjoyed a lot.If you are looking  for  the best antivirus software and support so you should try one of them which may help you a lot. Click below: 
mcafee.com/activate
mcafee.com/activate

McAfee Antivirus additionally squares malware that incorporates noxious sites, rootkits, Trojans, infections, and phishing plans. It gives you in-program reports about the dangers it squares. McAfee Antivirus records the IP tends to associated with the danger, which permits technically knowledgeable clients to maintain a strategic distance from sites associated with those addresses.
mcafee.com/activate | mcafee activate | install mcafee with activation code

mcafee.com/activate as antivirus of virus furnishes you with the security shields to prevent malicious documents from entering in the Desktop, PC/cell phone by means of malware, spyware, rootkit, and Trojan ponies. As individuals all around the globe complete their regular work on their official and PCs, they require absolute protection from a wide range of dangers that these infection records can cause. These infections are produced abruptly from old PC-stuck records or dubious Trojan pony, spyware, and malware online sites.
mcafee.com/activate | www.mcafee.com/activate | mcafee download | mcafee activate

Hey its john,I joned this communities for purpose of promote my technical site.Norton setup is where do you enter the Norton Setup Key at norton.com/setup to enact and introduce Norton item. Norton password generatot tools avoilable on Norton website : Norton setup
norton.com/setup || norton download | norton login | norton.com/setup | Norton setup | norton password generator

office.com/setup : Microsoft Office is utilized by the greater part of the individuals to complete their work in an orderly and sorted out way. office setup comes with various adaptations like Office 2019, Office 2016, Office 2013, and Office 365. Each Microsoft program is utilized for various assignments. Every adaptation of the Microsoft Office Setup needs an item code to complete its actuation procedure. Microsoft Office is utilized for both the reason whether it is for home or business. This assumes a significant job on the off chance that you like to keep your work in an efficient manner : microsoft redeem code

You could change someone's life by such an unselfish act Your content is accurate well versed with technology idea.Actually i enjoyed a lot.If you are looking  for  the best antivirus software and support so you should try one of them which may help you a lot. Click below: 
mcafee.com/activate
mcafee.com/activate

Accounting paper writing services are essential and they have become very popular for those seeking accounting coursework writing services since most of them seek Accounting Writing Services.

There are many Online Coursework writing services and Help with Coursework Writing services to choose from for those stuck with their psychology coursework writing services and nursing coursework writing help services.

Post a Comment