[英]ASP.NET MVC Custom dropdown from model with custom HtmlHelper
I am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns based on passing in a model ( any ) what the property is for the value and same for text. 我只是在MVC中使用HtmlHelpers玩,非常有用的东西,现在我试图基于传入模型(任意)为值创建属性,并为文本提供相同属性,从而为下拉列表创建一个。
so i have something like this: 所以我有这样的事情:
public static MvcHtmlString DataFilledDropDown<T>(this HtmlHelper h, string name, T selectedValue)
{
var myModel = typeof(T);
var dropDownBox = new Tag("select").With("name", name).And("id", name);
foreach (T val in Enum.GetValues(myModel))
{
var itemText = Resources.ResourceManager.GetString(val.ToString());
if (String.IsNullOrEmpty(itemText)) itemText = val.ToString();
var dft = new Tag("option")
.With("value", "0")
.AndIf(val.Equals(selectedValue), "selected", "selected")
.WithText("-- CHOOSE --");
dropDownBox.Append(dft);
var option = new Tag("option")
.With("value", (val).ToString())
.AndIf(val.Equals(selectedValue), "selected", "selected")
.WithText(itemText);
dropDownBox.Append(option);
}
return MvcHtmlString.Create(dropDownBox.ToString());
}
but this would only cover me for single fields but my model could be like this: 但这仅涵盖单个字段,但我的模型可能是这样的:
public class TestModel{
public int Id {get; set; } // this i want as a value in dropdown
public string Name {get;set;} // this i want to be the text value in dropdown
public bool isActive { get; set; } // this i dont need in dropdown but have data in model
}
so with above i want to then create something like this: 因此,在上面我想创建如下内容:
Html.DataFilledDropDown<myModel>("TestDropdown","Id","Name","0")
where the input is name of dropdown, name of value property, name of text property and default selected value 输入的是下拉列表的名称,值属性的名称,文本属性的名称和默认选择的值
I have completed this with a little help from the answer below, this is the code for anyone interested: 我已经从下面的答案中获得了一些帮助,完成了此操作,这是任何有兴趣的人的代码:
public static MvcHtmlString DataFilledDropDown<T>(
this HtmlHelper h, string name, IEnumerable<T> items,
Func<T,string> valueField, Func<T,string> textField, string selectedValue)
{
var dropDownBox = new Tag("select").With("name", name).And("id", name);
var defaultValue = "0";
var dft = new Tag("option")
.With("value", "0")
.AndIf(defaultValue.Equals(selectedValue), "selected", "selected")
.WithText("-- CHOOSE --");
dropDownBox.Append(dft);
foreach (var item in items)
{
var myValue = valueField(item);
var myName = textField(item);
var option = new Tag("option")
.With("value", myValue)
.AndIf(myValue.Equals(selectedValue), "selected", "selected")
.WithText(myName);
dropDownBox.Append(option);
}
return MvcHtmlString.Create(dropDownBox.ToString());
}
and to run the code 并运行代码
<%: Html.DataFilledDropDown("SpexOptionType", Model.Clubs, x => x.clubID.ToString(), x => x.clubName, "0")%>
thats it 而已
many thanks 非常感谢
I know this doesn't answer your question directly, but I would suggest you move away from using strings for your name and ids. 我知道这不能直接回答您的问题,但我建议您不要使用字符串作为名称和ID。 They are problematic and hard to maintain over time. 它们存在问题,并且随着时间的推移难以维护。 Instead use Func. 而是使用Func。
Here's what I use for select lists: 这是我用于选择列表的方法:
public static string SelectList<T>(this HtmlHelper html, T root, IEnumerable<T> items, Func<T, string> itemValueContent,
Func<T, string> itemTextContent, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> parentProperty, string selectSize)
{
StringBuilder parentSb = new StringBuilder();
StringBuilder childSb = new StringBuilder();
parentSb.AppendFormat("<select class='parent' name='parent' size='{0}'>\r\n", selectSize);
childSb.AppendFormat("<select class='child' id='child' size='{0}'>\r\n", selectSize);
foreach (T parentItem in items)
{
foreach (T item in childrenProperty(parentItem))
{
RenderParentOption(parentSb, item, itemValueContent, itemTextContent);
foreach (T item1 in childrenProperty(item))
{
RenderOptionWithClass(childSb, item1, itemValueContent, itemTextContent, parentProperty);
}
}
}
parentSb.AppendLine("</select>");
childSb.AppendLine("</select>");
return parentSb.ToString() + childSb.ToString();
}
private static void RenderOptionWithClass<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent, Func<T, string> parentProperty)
{
sb.AppendFormat("<option class='sub_{2}' value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item), parentProperty(item));
}
private static void RenderParentOption<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent)
{
sb.AppendFormat("<option value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item) + " ->");
}
This is how it's used: 使用方法如下:
<%=Html.SelectList<HierarchyNode<CategoryModel>>(Model.CategoryList.First(), Model.CategoryList,
x => x.Entity.RowId.ToString(),
x => x.Entity.Name.ToString(),
x => x.ChildNodes,
x => x.Entity.ParentCategoryId.ToString(), "10")%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.