I tried various options to solve this. Passed UpperCase values forcefully. Most surprisingly when I passed 'true' for all the radio buttons as follows, even then the output was same, buttons were checked only for uppercase url.
Up vote 4 down vote favorite share g+ share fb share tw.
Apologies for lots of code in here...I've tried to keep it as short as possible: I have written (stolen mostly from here) a HtmlHelper extension to write out a RadioButton (grouped) based upon an enum. I have also set my route map so it uses an enum instead of "id" etc. I have two enum's (for this example) CurrencyType and StatusType When I call the URL /GBP/Open, my HtmlHelper works correctly and sets the value of the radio buttons with GBP and Open checked. When I call the URL /gbp/open - the Helper still checks the values and appears to work...but when RadioButton().ToHtmlString() is called the "checked" is not present?
With the default routing in place, the radio buttons are still set correctly too? If you breakpoint the returned SelectList then you can see the option selected is set correctly, so I'm a little stumped as to where the problem is arising? Empty MVC3 website called Area36...Area 51 was taken ;) using System; using System.Collections.
Generic; using System. Linq; using System. Web; using System.Web.
Mvc; using System.Web.Mvc. Html; using System.Linq. Expressions; using System.
Text; namespace Area36. Models { public static class Extensions { public static SelectList ToSelectList(this TEnum enumObj) { var values = from TEnum e in Enum. GetValues(typeof(TEnum)) select new { ID = e, Name = e }; return new SelectList(values, "Id", "Name", enumObj); } public static void AppendFormatLine(this StringBuilder sb, string format, params object args) { sb.
AppendFormat(format, args); sb.AppendLine(); } public static MvcHtmlString RadioButtonForEnum(this HtmlHelper htmlHelper, Expression> expression, string fieldSet) { var metaData = ModelMetadata. FromLambdaExpression(expression, htmlHelper. ViewData); var e = (TProperty)Enum.
Parse(typeof(TProperty), metaData.Model.ToString()); var selectList = e.ToSelectList(); var sb = new StringBuilder(); if (selectList! = null) { sb. AppendFormatLine("{0}", fieldSet); foreach (SelectListItem item in selectList) { var id = string.
Format("{0}_{1}", metaData. PropertyName, item. Value); if (htmlHelper.ViewData.TemplateInfo.
HtmlFieldPrefix! = string. Empty) id.
Insert(0, string. Format("{0}_", htmlHelper.ViewData.TemplateInfo. HtmlFieldPrefix)); var label = htmlHelper.
Label(id, HttpUtility. HtmlEncode(item. Text)); var radio = htmlHelper.
RadioButton(name: metaData. PropertyName, value: item. Value, isChecked: item.
Selected, htmlAttributes: new { id = id }).ToHtmlString(); sb. AppendFormatLine("{1}{2}", metaData. PropertyName, radio, label); } sb.
AppendLine(""); } return MvcHtmlString. Create(sb.ToString()); } } } using System; using System.Collections. Generic; using System.
Linq; using System. Web; using System.Web. Mvc; using Area36.
Models; namespace Area36. Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index(CurrencyType currency, StatusType status) { return View(new ViewModel { Currency = currency, Status = status }); } } } using System; using System.Collections. Generic; using System.
Linq; using System. Web; namespace Area36. Models { public class ViewModel { public CurrencyType Currency { get; set; } public StatusType Status { get; set; } } } @using Area36.
Models @model Area36.Models. ViewModel @{ ViewBag. Title = "Index"; } Index @Html.
RadioButtonForEnum(m => m. Currency, "Currency") @Html. RadioButtonForEnum(m => m.
Status, "Status") public static void RegisterRoutes(RouteCollection routes) { routes. IgnoreRoute("{resource}. Axd/{*pathInfo}"); routes.
MapRoute( "Default", // Route name "{currency}/{status}", // URL with parameters new { controller = "Home", action = "Index", currency = CurrencyType. EUR, status = StatusType. Closed} // Parameter defaults ); } c# asp.net-mvc-3 enums radio-button html-helper link|improve this question edited Sep 13 '11 at 10:03 asked Sep 13 '11 at 9:07BlueChippy636220 99% accept rate.
I tried various options to solve this. Passed UpperCase values forcefully. Most surprisingly when I passed 'true' for all the radio buttons as follows, even then the output was same, buttons were checked only for uppercase url.
Var radio = htmlHelper. RadioButton(metaData. PropertyName, item.
Value, true, new { id = id }).ToHtmlString(); With some rnd with this code and some googling I found this. If you check Following is code from MVC3-rtm. Here you can see value of isChecked is modified depending on ModelState and if you check the value of modelState.Valuesindex.
AttemptedValue in debug mode, it is obviously in lowercase (for "gbp/open"). That's why lowercase url doesn't render checked attribute. Private IHtmlString BuildRadioButton(string name, object value, bool?
IsChecked, IDictionary attributes) { string valueString = ConvertTo(value, typeof(string)) as string; TagBuilder builder = new TagBuilder("input"); builder. MergeAttribute("type", "radio", true); builder. GenerateId(name); builder.
MergeAttributes(attributes, replaceExisting: true); builder. MergeAttribute("value", valueString, replaceExisting: true); builder. MergeAttribute("name", name, replaceExisting: true); var modelState = ModelStatename; string modelValue = null; if (modelState!
= null) { modelValue = ConvertTo(modelState. Value, typeof(string)) as string; isChecked = isChecked? String.
Equals(modelValue, valueString, StringComparison. OrdinalIgnoreCase); } if (isChecked. HasValue) { // Overrides attribute values if (isChecked.
Value) { builder. MergeAttribute("checked", "checked", true); } else { builder.Attributes. Remove("checked"); } } AddErrorClass(builder, name); return builder.
ToHtmlString(TagRenderMode. SelfClosing); }.
Thank you :) With this knowledge, I've added a check on the resulting string and if the item is checked but doesn't contain "checked=checked" then I manually add it. – BlueChippy Sep 14 '11 at 4:49.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.