This repository has been archived by the owner on Jan 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the required hidden form field to CheckBoxFor(), resolves #68
- Loading branch information
1 parent
282ea75
commit 09bf217
Showing
4 changed files
with
54 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters