Shawn
Shawn

Reputation: 2435

Getting a value from a model within a model using razor in mvc

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())

Upvotes: 0

Views: 316

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you can access like this:

@{

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

    }
 }


}

}

Upvotes: 1

Related Questions