Create HTML Helpers Extension Method to Get Id and Name in jQuery Function

This article introduces two extension methods of the HtmlHelper class, one that obtains the Id in a jQuery function and another obtains the Name in a jQuery function. You create a strongly typed view then a field Id and Name are automatically created the same as a property name bound with the input field but we need the Id or Name of the input field in JavaScript to get the values. To get an Id or Name in a JavaScript function we write a hardcoded string. This article provides a solution for that hard-coded string, in other words, you can use a HTML Helper extension method in a JavaScript/jQuery function that takes the same model property as taken by the input field. If you want to learn about extension methods then please go through the Extension Method In C# article.

HTML Helper Extension Method to Get Id

Let's create a static class "HtmlExtension" that has an extension method for HTML Helper. After that it creates a method in this class to get the Id of the input field in the JavaScript function using the model class property.

  1. using System;  
  2. using System.Linq.Expressions;  
  3. using System.Web.Mvc;  
  4. namespace MvcHtmlHelperExample  
  5. {  
  6.     public static class HtmlExtension  
  7.     {  
  8.         public static string FieldIdFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)  
  9.         {  
  10.             var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));  
  11.             return id.Replace('[''_').Replace(']''_');  
  12.         }  
  13.     }  
  14. }

After that, I create a model that binds with view.

  1. namespace MvcHtmlHelperExample.Models  
  2. {  
  3.     public class Teacher  
  4.     {  
  5.         public string Name { getset; }  
  6.     }  
  7. } 

Now create a controller that has an action method for the HTTP get request.

  1. using System.Web.Mvc;  
  2. namespace MvcHtmlHelperExample.Controllers  
  3. {  
  4.     public class TeacherController : Controller  
  5.     {  
  6.         public ActionResult Index()  
  7.         {  
  8.             return View();  
  9.         }  
  10.     }  
  11. } 

Thereafter create a strong view that the input field binds with the model and create a JavaScript function that uses the HTML Helper extension method FieldIdFor() to get the Id of the input field and shows the input field's value in an alert message.

  1. @model MvcHtmlHelperExample.Models.Teacher  
  2. @using MvcHtmlHelperExample  
  3. @{  
  4. ViewBag.Title = "Index";  
  5. }  
  6. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>  
  7. <script type="text/javascript">  
  8.     function GetName() {  
  9.         var name = $("#@Html.FieldIdFor(model => model.Name)").val();  
  10.         alert(name)  
  11.     }  
  12. </script>  
  13. @using (Html.BeginForm()) {  
  14. <fieldset>  
  15. <legend>Teacher</legend>  
  16. <div class="editor-label">  
  17. @Html.LabelFor(model => model.Name)  
  18. </div>  
  19. <div class="editor-field">  
  20. @Html.EditorFor(model => model.Name)  
  21. </div>  
  22. <p>  
  23. <input type="submit" value="Create" onclick="GetName()"/>  
  24. </p>  
  25. </fieldset>  
  26. } 

Run the application and get the value in an alert message of the name field.

MVC.jpg
Figure 1.1: Output Screen

HTML Helper Extension Method to Get Name

Now create another method in the "HtmlExtension" class to get the name of the input field in the JavaScript function using the model class property.

  1. public static string FieldNameFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)  
  2. {  
  3.     return html.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));  
  4. } 

I am using previous Teacher model so no need to create new model. Now create a controller action for HTTP Get Request type.

  1. public ActionResult Teacher()  
  2. {  
  3.     return View();  
  4. } 

Thereafter create a strong view that the input field binds with the model and create a JavaScript function that uses the HTML Helper extension method FieldNameFor() to get the Name of the input field and shows the input field value in an alert message.

  1. @model MvcHtmlHelperExample.Models.Teacher  
  2. @using MvcHtmlHelperExample  
  3. @{  
  4. ViewBag.Title = "Teacher";  
  5. }  
  6. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>  
  7. <script type="text/javascript">  
  8.     function GetName() {  
  9.         var name = $("#@Html.FieldNameFor(model => model.Name)").val();  
  10.         alert(name)  
  11.     }  
  12. </script>  
  13. @using (Html.BeginForm()) {  
  14. <fieldset>  
  15. <legend>Teacher</legend>  
  16. <div class="editor-label">  
  17. @Html.LabelFor(model => model.Name):  
  18. </div>  
  19. <div class="editor-field">  
  20. @Html.EditorFor(model => model.Name)  
  21. </div>  
  22. <p>  
  23. <input type="submit" value="Create" onclick="GetName()"/>  
  24. </p>  
  25. </fieldset>  
  26. } 

Run the application and we get the same result as in Figure 1.1. Download the zip folder for the entire source code.


Similar Articles