Skip to content
This repository has been archived by the owner on Jan 11, 2018. It is now read-only.

Commit

Permalink
Adds the required hidden form field to CheckBoxFor(), resolves #68
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Nov 29, 2016
1 parent 282ea75 commit 09bf217
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions FluentBootstrap.Mvc/FluentBootstrap.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Forms\CheckBoxFor.cs" />
<Compile Include="Forms\FormControlFor.cs" />
<Compile Include="Forms\FormControlForBase.cs" />
<Compile Include="Forms\FormControlListFor.cs" />
Expand Down
47 changes: 47 additions & 0 deletions FluentBootstrap.Mvc/Forms/CheckBoxFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using FluentBootstrap.Mvc.Internals;

namespace FluentBootstrap.Mvc.Forms
{
public class CheckBoxFor<TModel, TValue> : Component
{
private readonly Expression<Func<TModel, TValue>> _expression;
private readonly bool _isNameInLabel;
private string _name;

internal CheckBoxFor(BootstrapHelper helper, Expression<Func<TModel, TValue>> expression, bool isNameInLabel = true)
: base(helper)
{
_expression = expression;
_isNameInLabel = isNameInLabel;
}

protected override void OnStart(TextWriter writer)
{
base.OnStart(writer);

ModelMetadata metadata = ModelMetadata.FromLambdaExpression(_expression, this.GetHtmlHelper<TModel>().ViewData);
string expressionText = ExpressionHelper.GetExpressionText(_expression);
_name = MvcFormExtensions.GetControlName(this.GetHelper<TModel>(), expressionText);
string label = MvcFormExtensions.GetControlLabel(metadata, expressionText);
bool isChecked = false;
if (metadata.Model == null || !bool.TryParse(metadata.Model.ToString(), out isChecked))
{
isChecked = false;
}
writer.Write(_isNameInLabel
? GetHelper().CheckBox(_name, label, null, isChecked).AddAttribute("value", isChecked)
: GetHelper().CheckBox(_name, null, label, isChecked).AddAttribute("value", isChecked));
}

protected override void OnFinish(TextWriter writer)
{
writer.Write(GetHelper().Hidden(_name, false));
base.OnFinish(writer);
}
}
}
1 change: 1 addition & 0 deletions FluentBootstrap.Mvc/Forms/HiddenFor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc.Html;
using FluentBootstrap.Forms;
using FluentBootstrap.Mvc.Internals;

namespace FluentBootstrap.Mvc.Forms
Expand Down
19 changes: 5 additions & 14 deletions FluentBootstrap.Mvc/Forms/MvcFormExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,13 @@ public static ComponentBuilder<MvcBootstrapConfig<TModel>, Input> PasswordFor<TC
return helper.Input(name, label, null, null, FormInputType.Password);
}

public static ComponentBuilder<MvcBootstrapConfig<TModel>, CheckedControl> CheckBoxFor<TComponent, TModel, TValue>(
public static ComponentBuilder<MvcBootstrapConfig<TModel>, CheckBoxFor<TModel, TValue>> CheckBoxFor<TComponent, TModel, TValue>(
this BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, Expression<Func<TModel, TValue>> expression, bool isNameInLabel = true)
where TComponent : Component, ICanCreate<CheckedControl>
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.GetConfig().HtmlHelper.ViewData);
string expressionText = ExpressionHelper.GetExpressionText(expression);
string name = GetControlName(helper, expressionText);
string label = GetControlLabel(metadata, expressionText);
bool isChecked = false;
if (metadata.Model == null || !bool.TryParse(metadata.Model.ToString(), out isChecked))
{
isChecked = false;
}
return isNameInLabel ? helper.CheckBox(name, label, null, isChecked) : helper.CheckBox(name, null, label, isChecked);
return new ComponentBuilder<MvcBootstrapConfig<TModel>, CheckBoxFor<TModel, TValue>>(helper.GetConfig(), new CheckBoxFor<TModel, TValue>(helper, expression, isNameInLabel));
}

public static ComponentBuilder<MvcBootstrapConfig<TModel>, CheckedControl> RadioFor<TComponent, TModel, TValue>(
this BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, Expression<Func<TModel, TValue>> expression, object value = null, bool isNameInLabel = true)
where TComponent : Component, ICanCreate<CheckedControl>
Expand Down Expand Up @@ -429,13 +420,13 @@ private static ComponentBuilder<MvcBootstrapConfig<TModel>, ControlLabel> GetCon
return new MvcBootstrapHelper<TModel>(helper.GetConfig().HtmlHelper).ControlLabel(label).For(TagBuilder.CreateSanitizedId(name));
}

private static string GetControlName<TComponent, TModel>(BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, string expressionText)
internal static string GetControlName<TComponent, TModel>(BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, string expressionText)
where TComponent : Component
{
return helper.GetConfig().HtmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
}

private static string GetControlLabel(ModelMetadata metadata, string expressionText)
internal static string GetControlLabel(ModelMetadata metadata, string expressionText)
{
string label = metadata.DisplayName;
if (label == null)
Expand Down

0 comments on commit 09bf217

Please sign in to comment.