Web
Analytics
Inline HtmlHelpers in asp.net MVC 5 | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code.

Example1 :

step1. Create Controller and write an action , here I created a “helpers” controller and “InlineHelpers” Action.

public ActionResult InlineHelpers()
{
return View();
}
Step2. Add View in above action and add following code.
@{
Layout = null;
}
@helper allitemslist(string[] args)
{
    @foreach (var item in args) {
  • @item
  • }
} InlineHelpers
@allitemslist(new string[] { "BlackBook", "Unleashed", "Wrox" }); @allitemslist(new string[] { "Reading", "Writing", "Internet surfing" });

Example2

Step1. Add an action “InlineHelpersExample2” in above controller.

  public ActionResult InlineHelpersExample2()
 {
 ViewBag.trainings = new string[] { "asp.net", "php", "awcst" };
 ViewBag.trainers = new string[] { "maninder", "yogesh", "ravi" };
 return View();
 }

Step2. Add View and write following code.

 @{
 Layout = null;
}
@helper alldata(string[] args)
{
 
    @foreach (var item in args) {
  • @item
  • }
} InlineHelpersExample2
@alldata((string[])ViewBag.trainings) @alldata((string[])ViewBag.trainers)