我有一个极其简单的视图模型。
public class TellAFriendViewModel
{
public string Email1 { get; set; }
public string Email2 { get; set; }
public string Email3 { get; set; }
public string Email4 { get; set; }
public string Email5 { get; set; }
}然后在我的视图上输入相应的输入,但我想知道是否有更好的方法(例如循环)来编写类似于我的视图的TextBoxes:
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.TextBoxFor(vm => vm.Email1)
@Html.TextBoxFor(vm => vm.Email2)
@Html.TextBoxFor(vm => vm.Email3)
@Html.TextBoxFor(vm => vm.Email4)
@Html.TextBoxFor(vm => vm.Email5)
}发布于 2012-03-29 07:08:41
您可以使用反射遍历下面model...as的每个属性
Type type = Model.GetType(); // Model is the object you are binding with in your view
PropertyInfo[] properties = type.GetProperties();
foreach (var property in properties)
{
// Code to create your text box for the property
}希望这能帮到你。
https://stackoverflow.com/questions/9916870
复制相似问题