Open TDK1964 opened 8 years ago
What are you having trouble with?
I'm using Select2 as my DDL and wanted to create something like @Html.SelectBlock(m=>m.SomeFKId, null, null, i=>i.CurrentValue(SomeSavedValue))
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);
}
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);
}
Hi Jimmy,
I'm trying to create a select list that's not bound to EF. I want to have something like this:-
But I can't figure out how to do it. Would you by chance have a spare minute to help?