私は現在、asp.netコアmvcアプリケーションを書いています。私は.netコアに慣れています。私はタグヘルパーに興奮しており、私はそれらの周りに私の脳をラップしようとしています。私が得て、カップルを作ったシンプルなものが、実際に必要なのは<input type="checkbox">
をbootstrap toggleに置き換えることです。asp.netコアmvcタグヘルパーブートストラップトグル
私は私のカスタムを使用して作業トグル「ブール」タグヘルパーの表示を取得することができた
<div class="form-group">
<label asp-for="IsBusiness"></label>
<boolean asp-for="IsBusiness" class="form-control"/>
<span asp-validation-for="IsBusiness" class="text-danger"></span>
</div>
しかし、私は、チェックボックスの値を保存しているが午前問題/トグルする際、ユーザーのターンそれをオンにしてから[保存]をクリックします。フォームの検証で"The value 'on' is not valid for Login Is A Business?."
というエラーが表示されます。
誰でも私がこれを理解するのを手伝ってください、これは隠されたフィールドか何かを必要とするでしょうか?ここで
は、これまでのところ、私のタグヘルパーです:
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Bidz4Hire.Utility.TagHelpers
{
// Creates <input type="checkbox" data-toggle="toggle" />
[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
}
}
}