0

I am trying to use array to loop through model fields @Html.DisplayNameFor(model => model.[fld]) where fld is in an array flds see code below. Please let me know how to do this.

@model IEnumerable<SchoolAutomationSite.Models.tblClass>

    @{
        ViewBag.Title = "List Of Classes";
    }

    <h2>@ViewBag.Title</h2>

    <p>
        @Html.ActionLink("Create New Class", "Create")
    </p>

    <table class="table">
        <tr>

This is the Array

    @{
        string[] DisplayName = { "name", "description", "createdAt", "updatedAt" };
    }


    @foreach (var fld in flds)
    {

            <th>
                @Html.DisplayNameFor(model => model.[fld]) 

How to do the above line?

            </th>
    }
        </tr>

    @foreach (var item in Model) {
        <tr>

            @foreach (var fld in flds)
            {

                <td>
                    @Html.DisplayFor(modelItem => item.[flds])
                </td>
            }


            use the above instead off
            ----------
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.createdAt)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.updatedAt)
            </td>

            ----------
        </tr>
    }

    </table>
tereško
  • 58,060
  • 25
  • 98
  • 150
Ajashnal
  • 15
  • 1
  • 3
  • the array is "string[] flds" not "string[] DisplayName". So that is not problem – Ajashnal Sep 28 '15 at 02:46
  • 1
    This just makes your view code less readable, why not just stick with the more verbose code? – DavidG Sep 28 '15 at 02:52
  • It would be just `@foreach (var fld in flds) { @fld }` - but why do this? –  Sep 28 '15 at 03:16
  • First I need to get the DisplayNameFor using the array in the headers. Second I need to use the array to get the row values. Also than I can just add any new flds in the array without changing the code below. – Ajashnal Sep 28 '15 at 03:59

1 Answers1

0

You can convert a dotNet object to a dictionary and then iterate through the keys collection. Here's an extension method I've used in the past:

    public static class ObjectExtensions
{
    /// <summary>
    /// Turns object into dictionary
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static IDictionary<string, TVal> ToDictionary<TVal>(this object o)
    {
        if (o != null)
        {
            var props = TypeDescriptor.GetProperties(o);
            var d = new Dictionary<string, TVal>();
            foreach (var prop in props.Cast<PropertyDescriptor>())
            {
                var val = prop.GetValue(o);
                if (val != null)
                {
                    d.Add(prop.Name, (TVal)val);
                }
            }
            return d;
        }
        return new Dictionary<string, TVal>();
    }

}

And then call it like this:

var fields = Model.ToDictionary();
foreach (var key in fields.Keys) {
    <td>
        @fields[key]
    </td>
}

or, using your flds array:

var fields = Model.ToDictionary();
foreach (var fld in flds) {
    <td>
        @fields[fld]
    </td>
}
Robert Foster
  • 2,317
  • 18
  • 28
  • You probably want to filter the properties too depending on the model/object you're trying to iterate through... – Robert Foster Sep 28 '15 at 03:09
  • I was looking for a simple solution. Also it doesnt really answer the question – Ajashnal Sep 28 '15 at 04:00
  • It does provide you with a way to iterate through the properties on a model; isn't that what you wanted? Once you've got that you can do what you like with the properties/values... – Robert Foster Sep 28 '15 at 04:11
  • You could also look at this StackOverflow question for an alternative: http://stackoverflow.com/questions/957783/loop-through-an-objects-properties-in-c-sharp – Robert Foster Sep 28 '15 at 04:15