Foreach does not find a nested class

I want to create a model for the mvc page as follows:

public class Body
{
    public int Id { get; set; }

    public class Hand
    {
        public List<Fingers> fingers { get; set; }
    }

    public class Foot
    {
        public List<Toes> toes { get; set; }
    }

    public class Head
    {
        public Nose nose {get; set;}
        public List<Ears> ears { get; set; }
        public List<Eyes> eyes { get; set; }
    }
}

Then for the Fingers class:

public class Fingers
{
    public int Id { get; set; }
    public string Description { get; set; }
}

Then access it like this, in my opinion:

@model Models.Body
@foreach (var fingers in Model.Hand.Fingers)
{
    @Html.RadioButton("fingerList", fingers.Description)     
}

Am I doing my model wrong? Now VS does not recognize Model.Handin foreach, and even more so Model.Hand.Fingers. I do not want to do @model IEnumerable, because this page should show only one person, but can have several lists fingers, toesetc.

+4
source share
5 answers

There is Bodyno property in your class Hand.

, Hand fingers, fingers.

, , , Body , ( ):

public class Body
{
    public int Id { get; set; }
    public Hand hand { get; set; }
    public Foot foot { get; set; }
    public Head head { get; set; }

    public class Hand
    {
        public List<Fingers> fingers { get; set; }
    }

    public class Foot
    {
        public List<Toes> toes { get; set; }
    }

    public class Head
    {
        public Nose nose {get; set;}
        public List<Ears> ears { get; set; }
        public List<Eyes> eyes { get; set; }
    }
}

, , :

@model Models.Body
@foreach (var fingers in Model.hand.fingers)
{
    @Html.RadioButton("fingerList", fingers.Description)     
}

, / , .

+6

Hand , , Hand Body.

public class Hand
{
    public List<Fingers> Fingers { get; set; }
}
public class Body
{
    public int Id { get; set; }    
    public Hand Hand {set;get;}
    public Foot Foot { set;get;}
    public Head Head { set;get;}
}
public class Foot
{
    public List<Toes> toes { get; set; }
}
public class Head
{
   public Nose nose {get; set;}
   public List<Ears> ears { get; set; }
   public List<Eyes> eyes { get; set; }
}

GET Hand

public ActionResult Index()
{
  var vm = new Body { Hand= new Hand { Fingers = new List<Finger>()} };
  return View(vm);
}
+2

, Fingers Body Hand:

public List<Fingers> Fingers { get; set; }

:

@foreach (var fingers in Model.Fingers)
{
    @Html.RadioButton("fingerList", fingers.Description)     
}
+2

Body!?

public class Body
{
    public int Id { get; set; }

    public Hand Hand {get;set;}
    public Foot Foot {get;set;}
    public Head Head {get;set;}
}
+2

, - ; . ...

public class Hand
{
    public Hand()
    {
      Fingers = new List<Fingers>();
    }
}

Hand. , null, foreach loop .

: . Finger Toe, .

+2

All Articles