我目前有一个下拉列表,用于从SQL Server表中提取值。我希望用户能够拉出值和/或能够添加或编辑他们选择的值。我希望有一个可编辑的下拉列表,因为我认为这是最好的选择。我创建了一个dropdownlist控件,但当前它与我的常规dropdownlist相同。我一直在网上搜索,但没有找到沙发人。我在想也许是Razor HTML Helper控件?每个人觉得这样做最好的是什么? (作为最后的选择,我可以使用Ajax),但是如果我能使用它,我真的希望有一个可编辑的下拉菜单。
// HERE IS MY CURRENT DDL
@Html.DropDownListFor(x => x.Description, (IEnumerable<SelectListItem>) ViewBag.DescriptionDDL,"", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Description, "",new { @class = "text-danger" })
// Here is my dropdown control I built that is just a normal dropdown so far /* Custom Editable DropDownList Control */
public static MvcHtmlString CustomDropDown<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression,
List<string> names, object htmlAttributes)
{
var fieldName = ExpressionHelper.GetExpressionText(expression);
var fullbindingName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName);
var fieldId = TagBuilder.CreateSanitizedId(fullbindingName);
var metaData = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var value = metaData.Model;
TagBuilder tagBuilder = new TagBuilder("select");
tagBuilder.Attributes.Add("name", fullbindingName);
tagBuilder.Attributes.Add("id", fieldId);
StringBuilder options = new StringBuilder();
options.AppendLine("<option value='0' > Select </option>");
for(int i=0; i<names.Count; i++)
{
string singleOption = "<option value = '" + names[i].ToString() + "'>" + names[i].ToString() + "</options>";
options.AppendLine(singleOption);
}
tagBuilder.InnerHtml = options.ToString();
foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(htmlAttributes))
{
tagBuilder.MergeAttribute(prop.Name.Replace('_', '-'), prop.GetValue(htmlAttributes).ToString(), true);
}
return new MvcHtmlString(tagBuilder.ToString());
}