Skip to content

Instantly share code, notes, and snippets.

@randomcodenz
Created May 9, 2011 23:15
Show Gist options
  • Save randomcodenz/963625 to your computer and use it in GitHub Desktop.
Save randomcodenz/963625 to your computer and use it in GitHub Desktop.
# // Renders a radio button list within the standard GoFetch form field markup including error messages and field hints
# // Parameters
# // name - the name of the form field (required)
# // label - the content of the label associated with the form field (required)
# // options - the options to display (required)
# // hint - the content of the field hint displayed under the form field (optional)
# // defaultoption - the default option that is selected if no other value is specified as selected
# // Remarks
# // options can be supplied as either an IDictionary<string,object> or as an object ( e.g. new { option=value, option2=anothervalue } ).
# // If options is a dictionary the key is the option text and the value is the option value. If options is an object, the
# // property name will be the option text and the property value the option value.
<default hint="null" type="string" />
<default defaultoption="null" type="object" />
<var radioButtons='Html.CreateRadioButtonList(name, options, defaultoption)' />
<var hasError="Html.Errors(name).HasErrors"/>
<p class="field-message?{hasError} error?{hasError}">
<span class="label">!{label}</span>
<for each="var option in radioButtons">
!{option.RadioButton( new { @class = "radio" } )}
!{option.Label( new { @class="radio" })}
</for>
<span if="hasError" class="validation-message">!{Html.Errors(name).ErrorMessage}</span>
<span if="!hint.IsNullOrEmpty()" class="field-hint">!{hint}</span>
</p>
using System.Collections.Generic;
namespace GoFetchV2.Web.ViewHelpers
{
public class RadioButtonList : List<RadioButtonListItem>
{
public RadioButtonListItem this[ string value ]
{
get
{
return Find( item => item.Value.Equals( value ) );
}
}
public RadioButtonList( IEnumerable<RadioButtonListItem> collection ) : base( collection ) {}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using System.Linq;
namespace GoFetchV2.Web.ViewHelpers
{
public static class RadioButtonListExtensions
{
public static RadioButtonList CreateRadioButtonList( this HtmlHelper htmlHelper, string name, IEnumerable<KeyValuePair<string, object>> options )
{
return htmlHelper.CreateRadioButtonList( name, options, null );
}
public static RadioButtonList CreateRadioButtonList( this HtmlHelper htmlHelper, string name, IEnumerable<KeyValuePair<string, object>> options, object defaultOption )
{
var selectedValue = htmlHelper.GetFieldValue( name, defaultOption );
var listItems = options
.Select(
option => new {Text = option.Key, Value = Convert.ToString( option.Value, CultureInfo.CurrentCulture )} )
.Select(( option, index ) => new RadioButtonListItem
{
Id = name + "_" + index,
Name = name,
Text = option.Text,
Value = option.Value,
Selected = String.Equals( selectedValue, option.Value, StringComparison.Ordinal )
});
return new RadioButtonList(listItems);
}
}
}
using System.Web.Mvc;
using System.Web.Routing;
namespace GoFetchV2.Web.ViewHelpers
{
public class RadioButtonListItem : SelectListItem
{
public string Id { get; set; }
public string Name { get; set; }
public string RadioButton()
{
return RadioButton( null );
}
public string RadioButton( object htmlAttributes )
{
var tagbuilder = new TagBuilder( "input" );
tagbuilder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
tagbuilder.MergeAttribute( "type", "radio", true );
tagbuilder.MergeAttribute( "name", Name, true );
tagbuilder.MergeAttribute( "value", Value );
tagbuilder.GenerateId( Id );
if(Selected)
{
tagbuilder.MergeAttribute( "checked", "checked" );
}
return tagbuilder.ToString( TagRenderMode.SelfClosing );
}
public string Label( object htmlAttributes )
{
var tagBuilder = new TagBuilder( "label" );
tagBuilder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
tagBuilder.MergeAttribute( "for", Id, true );
tagBuilder.InnerHtml = Text;
return tagBuilder.ToString( TagRenderMode.Normal );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment