title: asp.net core mvc tag helper for bootstrap toggle
body: <p>I am currently writing an asp.net core mvc app and I am new to .net core.  I am excited about tag helpers and I am trying to wrap my brain around them.  The simple ones I get and have made a couple, but the one that I really need is to replace the <code>&lt;input type="checkbox"&gt;</code> with <a href="http://www.bootstraptoggle.com/" rel="nofollow noreferrer">bootstrap toggle</a>.</p>
  
  <p>I have been able to get the display of the toggle working using my custom "boolean" tag helper </p>
  
  <pre><code>    &lt;div class="form-group"&gt;
          &lt;label asp-for="IsBusiness"&gt;&lt;/label&gt;
          &lt;boolean asp-for="IsBusiness" class="form-control"/&gt;
          &lt;span asp-validation-for="IsBusiness" class="text-danger"&gt;&lt;/span&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>However the problem I am having is saving the value of the checkbox/toggle when the user turns it on and then clicks save.  I am getting an error: <code>"The value 'on' is not valid for Login Is A Business?."</code> in the form validation.</p>
  
  <p>Can anyone please help me figure this out, is this going to need a hidden field or something?</p>
  
  <p>Here is my tag helper so far:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc.TagHelpers;
  using Microsoft.AspNetCore.Mvc.ViewFeatures;
  using Microsoft.AspNetCore.Razor.TagHelpers;
  
  namespace Bidz4Hire.Utility.TagHelpers
  {
      // Creates &lt;input type="checkbox" data-toggle="toggle" /&gt;
      [HtmlTargetElement("boolean")]
      public class BooleanTagHelper : InputTagHelper
      {
  
          public BooleanTagHelper(IHtmlGenerator generator) : base(generator)
          {
          }
  
          public string AspFor { get; set; }
  
          public override void Process(TagHelperContext context, TagHelperOutput output)
          {
          output.TagName = "input";
          output.TagMode = TagMode.SelfClosing;
  
          output.Attributes.SetAttribute("type", "checkbox");
          output.Attributes.SetAttribute("id", For.Name);
          output.Attributes.SetAttribute("name", For.Name);
          output.Attributes.SetAttribute("data-toggle", "toggle");
          output.Attributes.SetAttribute("data-on", "Yes");
          output.Attributes.SetAttribute("data-off", "No");
          output.Attributes.SetAttribute("value", "true");  //so the checked value returned is "true" not "on"
          if (Convert.ToBoolean(For.Model) == true) //check the model value
          {
              output.Attributes.SetAttribute("checked", "checked"); //turns the toggle to "Yes" initially
          }
      }
  }
  </code></pre>