jeudi 14 mai 2020

How to render only few properties of ViewModel in MVC?

I have following code. I am trying to render only FirstName and LastName using loop in View. How can I do using reflection MVC 5?

Model:

public class SomeViewModel
{
public int Id{get;set;}
public string FirstName{get;set;}
public string LastName{get;set;}
public string Address{get;set;}
}

View:

@foreach(var property in ViewData.ModelMetadata.Properties)
{
    <div class="editor-line">
        <label>@(property.DisplayName??property.PropertyName)</label>
        @Html.Editor(property.PropertyName)
    </div>
}




Aucun commentaire:

Enregistrer un commentaire