C# 循环遍历视图中的视图模型属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9916870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through view Model properties in a View
提问by Nick Brown
I have a painfully simple view model
我有一个非常简单的视图模型
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; }
}
And then the corresponding inputs on my view, but I'm wondering if there is a better way (such as a loop) to write similar TextBoxes to my view:
然后是我视图上的相应输入,但我想知道是否有更好的方法(例如循环)将类似的 TextBox 写入我的视图:
@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)
}
采纳答案by moribvndvs
You should access
你应该访问
ViewData.ModelMetadata.Properties. No reason to double the reflection effort, plus it figures out DataAttributes metadata for you.
ViewData.ModelMetadata.Properties. 没有理由将反射工作加倍,而且它会为您计算出 DataAttributes 元数据。
@foreach(var property in ViewData.ModelMetadata.Properties)
{
<div class="editor-line">
<label>@(property.DisplayName??property.PropertyName)</label>
@Html.Editor(property.PropertyName)
</div>
}
回答by Rob Rodi
you coulduse reflection over the properties, or a simple for loop to generate the same HTML as is generated in your solution, but what's your goal?
For simplicity, what you have wins.
您可以对属性使用反射,或者使用简单的 for 循环来生成与解决方案中生成的 HTML 相同的 HTML,但是您的目标是什么?
为简单起见,您拥有的获胜。
Reflection
反射
foreach (var prop in typeof(whatever).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
@Html.TextBox(prop.Name, prop.GetValue(Model));
}
Loop
环形
var numberProperties = 5; // you could also do typeof(whatever).GetProperties(BindingFlags.Instance | BindingFlags.Public).Count();
@for(var i = 0; i < numberProperties; i++){
<input type="text" name="Email@i" id="Email@i"/>
}
回答by Xavier Poinas
You should consider using an array.
您应该考虑使用数组。
However, ifyou wanted to go with reflection, it would look like this:
但是,如果您想使用反射,它看起来像这样:
@foreach (var prop in Model.GetType().GetProperties())
{
@(Html.TextBox(prop.Name, prop.GetValue(Model, null)))
}
回答by NiK
You can use Reflection to loop through each property of your model...as below
您可以使用反射来遍历模型的每个属性......如下
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
}
Hope this helps...
希望这可以帮助...
回答by Travis J
Perhaps like this?
也许像这样?
public class TellAFriendViewModel
{
List<string> Emails { get; set; }
public TellAFriendViewModel()
{
Emails = new List<string>(5);
}
}
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
@for(int count = 0 ; count < model.Emails.Count; count++)
{
@Html.TextBoxFor(vm => vm.Emails[count])
}
}
回答by RPM1984
Unless i'm missing something, I don't know why no-one has suggested this. Why is everyone looping and/or using reflection??
除非我遗漏了什么,否则我不知道为什么没有人建议这样做。为什么每个人都在循环和/或使用反射?
public class TellAFriendViewModel
{
public ICollection<EmailViewModel> Emails { get; set; } // populate 5 of them in ctor or controller
}
public class EmailViewModel
{
public string Email { get; set; }
}
View:
看法:
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.EditorFor(model => model.Emails)
}
EditorTemplates\EmailViewModel.cshtml
EditorTemplates\EmailViewModel.cshtml
@Html.TextBoxFor(model => model.Email)
Loops are NEVER required in MVC. I repeat. NEVER
MVC 中从不需要循环。我重复。绝不
回答by Rod Johnson
This is the accepted way to do it
这是公认的做法
foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => !pm.HideSurroundingHtml && pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
<div class="form-group">
<label>
@prop.GetDisplayName()
@if (prop.IsRequired)
{
<span class="required">*</span>
}
</label>
@Html.Editor(prop.PropertyName)
@Html.ValidationMessage(prop.PropertyName, new {@class = "help-block"})
</div>
}
}

