Updated on Kisan Patel
Create a C# class in any new folder (in this case “Utility”) of the application and write following code.
NAMESPACE:
using System.Web.Mvc;
CODE:
public static class CustomHelpers { public static MvcHtmlString Sum(this HtmlHelper html, int a, int b) { return new MvcHtmlString((a + b).ToString()); } }
Note the first parameter of the Sum method ie. HtmlHelper
that help us to call this method using @Html.
This creates an extension method called Sum that can be used in Razor syntax using @Html
.
<li>External Helper Method Output: <strong>@Html.Sum(15, 5000)</strong> </li>
Fore Detailed Example and Source Code Go to: Create Custom HTML Helper methods in ASP.NET MVC