Looping through view Model properties in a View

后端 未结 7 2252
南旧
南旧 2020-12-12 22:04

I have a painfully simple view model

public class TellAFriendViewModel
{
    public string Email1 { get; set; }
    public string Email2 { get; set; }
    pu         


        
相关标签:
7条回答
  • 2020-12-12 22:05

    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...

    0 讨论(0)
  • 2020-12-12 22:06

    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

    @Html.TextBoxFor(model => model.Email)
    

    Loops are NEVER required in MVC. I repeat. NEVER

    0 讨论(0)
  • 2020-12-12 22:09

    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>
    

    }

    0 讨论(0)
  • 2020-12-12 22:16

    You should access

    ViewData.ModelMetadata.Properties. No reason to double the reflection effort, plus it figures out DataAttributes metadata for you.

    @foreach(var property in ViewData.ModelMetadata.Properties)
    {
        <div class="editor-line">
            <label>@(property.DisplayName??property.PropertyName)</label>
            @Html.Editor(property.PropertyName)
        </div>
    }
    
    0 讨论(0)
  • 2020-12-12 22:22

    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])
     }
    }
    
    0 讨论(0)
  • 2020-12-12 22:24

    You should consider using an array.

    However, if you 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)))
    }
    
    0 讨论(0)
提交回复
热议问题