0

I'm creating a custom radio button helper using tagbuilder. I'm making use of the steps suggested in the post: Custom helper for generating html tags for radio button and associated label , but have tweaked it a bit to pass the values to the radio button using List , somewhat like this:-

@{
    List<SelectListItem> inventory = new List<SelectListItem>();
    inventory.Add(new SelectListItem { Text = "True", Value = bool.TrueString, Selected = true });
    inventory.Add(new SelectListItem { Text = "False", Value = bool.FalseString});
}

But, even when I select true, it always takes the value as FALSE. Below, is my code for constructing tag builder, please let me know where I'm going wrong:

public static MvcHtmlString CustomRadioButtonFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, IEnumerable<SelectListItem> radioList)
        {
            StringBuilder radiobutton = new StringBuilder();
            TagBuilder radio = null;
            TagBuilder label = null;

            string[] propertyNameParts = expression.Body.ToString().Split('.');
            string propertyName = propertyNameParts.Last();
            string booleanStr = "";

            // get the value of the property
            Func<TModel, bool> compiled = expression.Compile();
            var name = ExpressionHelper.GetExpressionText(expression);
            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            if (metadata.Model != null)
            {

                booleanStr = Convert.ToString(compiled(htmlHelper.ViewData.Model));

            }
            else
                booleanStr = string.Empty;

            // convert it to a boolean
            bool isChecked = false;
            Boolean.TryParse(booleanStr, out isChecked);

            foreach (SelectListItem item in radioList)
            {
                radio = new TagBuilder("input");
                label = new TagBuilder("label");
                label.InnerHtml = item.Text;
                radio.Attributes.Add("type", "radio");
                radio.Attributes.Add("name", name);
                radio.Attributes.Add("value", booleanStr);
                radiobutton.Append(label.ToString());
                radiobutton.Append(radio.ToString());
            }
            return MvcHtmlString.Create(radiobutton.ToString());
        }

Any help would be appreciated. Thanks in advance!

Community
  • 1
  • 1
Newbie
  • 11
  • 5
  • This is crazy, Why on earth do you even need to pass a `SelectList` to your custom helper. Your not understanding how to create `HtmlHelper` extension methods in particular how to create correct 2-way model binding, and I strongly recommens you study the [source code](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/InputExtensions.cs) before going any further –  May 27 '16 at 07:16
  • As I'm referring to the approach suggested by http://20fingers2brains.blogspot.com/2013/04/custom-radiobutton-html-helper-in-mvc3.html. As you can see SelectList, is made use of over here. Can you suggest a work around to THIS ?? – Newbie May 27 '16 at 07:25
  • That blog is nonsense. Your helper should be just - `StringBuilder html = new StringBuilder(); html.Append(htmlHelper.RadioButtonFor(expression, true, new { id = "yes" })); // add label plus other radio button and label // return MvcHtmlString.Create(html .ToString());`(6 lines of code plus any other elements such as enclosing divs etc) –  May 27 '16 at 07:36
  • Yes, that's fine. But actually I wanted to avoid using the in-built htmlhelper methods like (htmlHelper.RadioButtonFor(..)) which are provided by MVC, and instead construct my own version of htmlhelper for radio button. Can you suggest any links I should refer to or what approach I ought to follow for it. – Newbie May 27 '16 at 08:58
  • Are you serious? You clearly have no understanding of what you doing based on this and your [previous question](http://stackoverflow.com/questions/37291175/modelmetadata-lambdaexpression-not-returning-correct-value-for-boolean-field). –  May 27 '16 at 10:17
  • Elaborate on what. Why in the world would you not use the inbuilt methods which do it correctly. All the code in this and your previous questions shows that you do not understand the basics of model binding. Are you seriously wanting to make a web site which will not work? –  May 27 '16 at 12:47
  • I need to build custom html helpers and do not want to make use of the inbuilt methods provided by MVC. This has been my sole objective all the while. Could you suggest any resources or links I could refer to or study, to implement it? – Newbie May 27 '16 at 13:25
  • I guess you just not understanding. You **are** building a custom `HtmlHelper` extension method (it outputs 2 radio buttons and its 2 associated labels), but you do it using the inbuilt methods to generate each part of its html. And I have already given you links to the source code. –  May 27 '16 at 22:37

0 Answers0