0

i have a model class like,

 public class Report {
    public string No { get; set; }
    public string FirstName { get; set; }
    NameValueCollection Data { get; set; }

        public Report() {
        Data = new NameValueCollection();
    }....
 }

And from my model i retrive all data list this particular model

     public ActionResult Search(string q) {
            var search = new Models.Search(q);
            return View(search.GetReportItems());
        }

var search contain all the items including "Data"

and my View is

@model List<...Models.Report>
@foreach (var item in Model)
        {
            <tr>
                <td>@item.No</td>
                <td>@item.FirstName </td>

                // here i want show the items in "Data "
            </tr>
        }

how can i show the items in that particular NameValueCollection . please help me

neel
  • 5,123
  • 12
  • 47
  • 67
  • Here's [an example](http://stackoverflow.com/questions/5065928/c-sharp-iterate-through-namevaluecollection) of iterating a `NameValueCollection`. – Andrei V Feb 13 '14 at 10:56
  • is that an exact copy of the Report class? Data isn't public property? – owenrumney Feb 13 '14 at 10:56
  • @owen79: Data contain many fields which is iterating from excel.. it is the var search – neel Feb 13 '14 at 11:01

1 Answers1

1

Here is anexample of how it can be done. Note that your actual markup might be (and most likely will be) different, depending on your particular needs.

<tr>
    <td>@item.No</td>
    <td>@item.FirstName </td>

    @foreach (String key in item.Data.AllKeys)
    {
        <td>@item.Data[key]</td>
    }
</tr>

Also you might have trouble accessing Data from outside of the class, as currently it is declared private.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • 1
    @Parvathiiiii, what error? Again, make sure you declared Data as public field if you want to access it in the markup – Andrei Feb 13 '14 at 11:04