Asp.net mvc Asp.net mvc3中动态视图中的下拉控件
我有一个模型Asp.net mvc Asp.net mvc3中动态视图中的下拉控件,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个模型 public class DynamicFields { public string propertyName { get; set; } public string propertyType { get; set; } } 这将作为我的控制器中的列表填充 现在,我需要根据propertyType文本值在视图中动态呈现控件。如果值为“TextBox”,我需要呈现TextBox控件。所以我用editor来控制 观点 @model IEnumerable @fo
public class DynamicFields
{
public string propertyName { get; set; }
public string propertyType { get; set; }
}
这将作为我的控制器中的列表填充
现在,我需要根据propertyType文本值在视图中动态呈现控件。如果值为“TextBox”,我需要呈现TextBox控件。所以我用editor来控制
观点
@model IEnumerable
@foreach(模型中的var字段)
{
@LabelFor(model=>field.propertyName)
@EditorFor(model=>field.propertyName,field.propertyType)
}
我不知道在DropDownList的情况下如何处理这个问题
提前感谢您的帮助。这是假设您希望在页面呈现上显示下拉列表,而不是文本框。
@model IEnumerable<DynamicFields>
@foreach (var field in Model)
{
Html.LabelFor(model => field.propertyName)
if (field.propertyType == "Textbox")
{
Html.EditorFor(model => field.propertyName)
}
else
{
Html.DropDownListFor(model => model.propertyName, field.propertyType)
}
}
@model IEnumerable
@foreach(模型中的var字段)
{
LabelFor(model=>field.propertyName)
如果(field.propertyType==“Textbox”)
{
EditorFor(model=>field.propertyName)
}
其他的
{
Html.DropDownListFor(model=>model.propertyName,field.propertyType)
}
}
如果希望根据用户在下拉列表中选择的内容在客户端进行更改,则必须使用javascript/jquery
@model IEnumerable<DynamicFields>
@foreach (var field in Model)
{
Html.LabelFor(model => field.propertyName)
if (field.propertyType == "Textbox")
{
Html.EditorFor(model => field.propertyName)
}
else
{
Html.DropDownListFor(model => model.propertyName, field.propertyType)
}
}