title: ASP.NET MVC - Using Model in Custom HTML Helper
body: <p>I am building an app with ASP.NET MVC and Bootstrap. In my app, I have a view with a Model that looks like this:</p>
  
  <pre><code>public class EntryModel
  {
    [Required(ErrorMessage="Please enter the name.")]
    [Display(Name="Name")]
    public string Name { get; set; }
  
    [Required (ErrorMessage="Please enter the description.")]
    [Display(Name = "Description")]
    public string Description { get; set; }
  }
  </code></pre>
  
  <p>In this app, I've also defined a custom html helper that looks like this:</p>
  
  <pre><code>public static class MyHelpers
  {
    public static MvcHtmlString MyTextBox(this HtmlHelper helper)
    {
      var sb = new StringBuilder();
      sb.Append("&lt;div class=\"form-group\"&gt;");
      sb.Append("&lt;label class=\"control-label\" for=\"[controlId]\"&gt;[labelValue]&lt;/label&gt;");
      sb.Append("&lt;input class=\"form-control\" id=\"[controlId]\" name=\"controlId\" type=\"text\" value=\"[propertyValue]\"&gt;");
      sb.Append("&lt;/div&gt;");
  
      return MvcHtmlString.Create(sb.ToString());
    }
  }
  </code></pre>
  
  <p>I'm using this helper and model in my Razor view, like this:</p>
  
  <pre><code>@model EntryModel
  &lt;h2&gt;Hello&lt;/h2&gt;
  
  @using (Html.BeginForm("Add", "Entry", new {}, FormMethod.Post, new { role="form" }))
  {
    @Html.MyTextBox() 
  }
  </code></pre>
  
  <p>I am trying to generate the <code>labelValue</code>, <code>controlId</code>, and <code>propertyValue</code> values in the helper from the properties of the Model. For example, I'd like to use <code>@Html.MyTextBoxFor(m =&gt; m.Name)</code> and have the helper generate something like this:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
    &lt;label class="control-label" for="Name"&gt;Name&lt;/label&gt;");
    &lt;input class="form-control" id="Name" name="Name" type="text" value="Jon"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Essentially, I'm not sure how to get my model information into my html helper.</p>