ContosoUniversity icon indicating copy to clipboard operation
ContosoUniversity copied to clipboard

Basic DropDownList

Open TDK1964 opened this issue 7 years ago • 4 comments

Hi Jimmy,

I'm trying to create a select list that's not bound to EF. I want to have something like this:-

public static HtmlTag Select<T>(this HtmlHelper<T> helper, Expression<Func<T, object>> expression)
            where T : class
        {
            var generator = GetGenerator(helper.ViewData.Model);
            return generator.SelectFor(expression, "Select");
        }

But I can't figure out how to do it. Would you by chance have a spare minute to help?

TDK1964 avatar Sep 14 '16 11:09 TDK1964

What are you having trouble with?

jbogard avatar Sep 22 '16 00:09 jbogard

I'm using Select2 as my DDL and wanted to create something like @Html.SelectBlock(m=>m.SomeFKId, null, null, i=>i.CurrentValue(SomeSavedValue))

TDK1964 avatar Oct 04 '16 10:10 TDK1964

Any thoughts on this Jimmy? I think the problem is this method always returns and input tag instead of a select tag.

public static HtmlTag Input<T>(this HtmlHelper<T> helper, Expression<Func<T, object>> expression) where T : class
        {
            var generator = GetGenerator(helper.ViewData.Model);
            return generator.InputFor(expression);
        }

TDK1964 avatar Oct 24 '16 11:10 TDK1964

I've managed to get it working to a fashion but it's not inline with your code. I just couldn't figure out how to do it any other way :(

        public static HtmlTag SelectBlock<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string url = null, int? defaultValue = null, string defaultText = null, Orientation orientation = Orientation.Horizontal)
        {
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var modelName = ExpressionHelper.GetExpressionText(expression);
            var labelText = metadata.DisplayName ?? modelName;

            var formGroup = new HtmlTag("div").AddClass("form-group");

            var label = new HtmlTag("label").AddClass("control-label").Attr("for", modelName).Text(labelText);

            var input = new HtmlTag("select").AddClass("select2").Attr("name", modelName).Style("width","250px");

            var option = new HtmlTag("option").Value("-1").Text("-- Select --").Attr("selected","selected");

            if (defaultValue > 0)
                option = new HtmlTag("option").Value(defaultValue).Text(defaultText).Attr("selected", "selected");

            input.Append(option);

            if (helper.ViewData.TemplateInfo.HtmlFieldPrefix != string.Empty)
            {
                input.Attr("id", helper.ViewData.TemplateInfo.HtmlFieldPrefix + "_" + input.Attr("name"));
                input.Attr("name", helper.ViewData.TemplateInfo.HtmlFieldPrefix + "." + input.Attr("name"));
            }

            var scriptTag = new HtmlTag("script").AppendHtml(SelectScriptBlockCode(input.Attr("id"), url, defaultValue, defaultText));

            return formGroup.Append(label).Append(input).Append(scriptTag);
        }

TDK1964 avatar Oct 24 '16 18:10 TDK1964