1

I created a custom HTMLHelper extension method for Razor

public static MvcHtmlString BootstrapButtonGroupForEnum<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
   // render stuff
}

And use it like this:

@Html.BootstrapButtonGroupForEnum(model => model.PriceType)

It works fine for adding records, but how to I pass the value of the model's method to it? So I can make a value preselected. Do I need to pass the value via an additional parameter or is there a more elegant way of doing this. I had a look in Watch and did not see the value there, did I miss something?

Thanks!

EDIT: The full implementation of the html helper:

public static MvcHtmlString BootstrapButtonGroupForEnum<TModel, TProperty>(
     this HtmlHelper<TModel> htmlHelper, 
     Expression<Func<TModel, TProperty>> expression)
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            string[] listOfNames = Enum.GetNames(metaData.ModelType);

            var sb = new StringBuilder();

            if (listOfNames != null)
            {

                sb = sb.Append("<div class=\"btn-group btn-group\" role=\"group\" >");

                foreach (var value in listOfNames)
                {

                    sb.Append("<button type=\"button\" class=\"btn btn-primary\" onclick=\"");

                    // onclick event hadler
                    sb.AppendFormat("$('#{0}').val('{1}');", metaData.PropertyName, value); // get the value to the hidden
                    sb.Append("$(this).addClass('active').siblings().removeClass('active');"); // retain selection

                    sb.Append("\">");
                    sb.Append(value.ToString().UnPascalCase());
                    sb.Append("</button>");

                }

                sb = sb.Append("</div>");

                sb = sb.AppendFormat("<input type=\"hidden\" id=\"{0}\" name=\"{0}\" />", metaData.PropertyName);

            }

            return MvcHtmlString.Create(sb.ToString());
        }

I suppose I could try using @HiddenFor(...) instead of rendering it directly as html?

Paceman
  • 2,095
  • 4
  • 20
  • 27
  • You can create a models Metadata from the expression, however the fact you think you need to do this suggests you not understanding how to create custom extension methods. You should be making use of the inbuilt extensions. Some example [here](http://stackoverflow.com/questions/26162218/editortemplate-for-dropdownlist/26417466#26417466) (option 3) and [here](http://stackoverflow.com/questions/27984973/enum-radiobuttonfor-editor-template-set-value/27995314#27995314). If your having problems, post your code. –  Aug 15 '15 at 00:31
  • My implementation was loosely based on this: http://www.jlum.ws/post/2013/11/5/displaying-enum-values-as-radio-button-options . I added the full implementation of the helper method in the EDIT. – Paceman Aug 15 '15 at 03:21
  • 1
    Firstly you should never output a script in a helper. This means that you will be duplicating scripts (not to mention polluting your markup with behavior is not good practice). Its hard to understand what you really want to do - your method is named `BootstrapButtonGroupForEnum` but nowhere are you generating radio buttons –  Aug 15 '15 at 03:35
  • +1 Point taken about outputting script, fully agreed. At this stage I am just trying to get the thing to work, once done I will definitely factor out the scripts and connect it via classes. – Paceman Aug 15 '15 at 04:37
  • 1
    Strongly suggest you explore the source code for some of the built in helpers to understand how it should be done. –  Aug 15 '15 at 04:40

1 Answers1

0

I needed to assign the value of the hidden to metaData.Model and take it from there.

 sb = sb.AppendFormat("<input type=\"hidden\" id=\"{0}\" name=\"{0}\" value=\"" + metaData.Model + "\" />", metaData.PropertyName);

CORRECTION: After following Stephens advice and looking at MVC source code I concluded that the hidden should be rendered like this:

var hidden = InputExtensions.HiddenFor(htmlHelper, expression, null);
sb.Append(hidden );
Paceman
  • 2,095
  • 4
  • 20
  • 27