0

I am having an issue on how to a value out of a model within a model and accessing it in razor. I have this model Problems that contains a model called Types. How would I go about accessing values from Type through Problems in razor. It would look something like this, but obviously this does not work?

This is how I call the model in the view:

@model IEnumerable<MyApp.Models.Problems>

trying to access Type values in this model that hold Types.

Here is my model where I am trying to access the values:

public class Problems
    {
        public int Id { get; set; }
        public string Title { get; set; }

        public IEnumerable<MyApp.Models.Type> Types {get; set;}

    }

This is my other model I am trying to access the values from:

public partial class Type
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }

So something I would try and do would look like this, but this obviously doesn't work.

@Html.DisplayNameFor(model => model.Type.Description())
tereško
  • 58,060
  • 25
  • 98
  • 150
Shawn
  • 2,355
  • 14
  • 48
  • 98

1 Answers1

1

you can access like this:

@{

foreach(var item in Model)
{
  if(item.Types.Count() > 0)
  {
    foreach(var innerItem in item.Types)
    {

    }
 }


}

}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • I see.... using nested foreach loops. How would I call it in the razor tag?@Html.DisplayNameFor(inneritem => inneritem.Title). But it wont let me use the same name? – Shawn Apr 24 '14 at 04:46
  • 1
    refer thiss one:http://stackoverflow.com/questions/8894442/mvc-razor-view-nested-foreachs-model – Ehsan Sajjad Apr 24 '14 at 04:56