4

I want to make a model for mvc page like this:

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 have a class for Fingers like this:

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

Then access it like this in my view:

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

Am I doing my model wrong? Right now VS doesn't recognize the Model.Hand in the foreach, much less the Model.Hand.Fingers. I don't want to make the @model IEnumerable because this page should only show one person, but it can have multiple lists of fingers, toes, etc.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
tshoemake
  • 1,311
  • 1
  • 17
  • 28

5 Answers5

6

Your Body class has no property Hand on it.

Beyond that, your Hand class has fingers but in razor you refer to it with Fingers.

I don't know if you really intend to have your classes be nested, but you need to add properties onto your Body class for the ones you want (sticking with your property capitalization conventions):

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; }
    }
}

and then you need to refer to the properties with casing that matches your properties, not the class names:

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

I would also argue that your casing/capitalization is incorrect, but that's not the point of your question.

Tim
  • 14,999
  • 1
  • 45
  • 68
  • What are the advantages/disadvantages of having them nested – tshoemake Apr 29 '16 at 17:34
  • That's a discussion for another thread. But here's a rabbit hole you can go down: http://stackoverflow.com/questions/1083032/why-would-i-ever-need-to-use-c-sharp-nested-classes – Tim Apr 29 '16 at 17:35
2

Since you are accessing the Hand property of your model which is Body type, You should add a property called Hand to your Body class.

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; }
}

Now in your GET action, you need to initialize the Hand property

public ActionResult Index()
{
  var vm = new Body { Hand= new Hand { Fingers = new List<Finger>()} };
  return View(vm);
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
2

I think you need a property called Fingers in your Body class instead of a nested class called Hand:

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

Then you could:

@foreach (var fingers in Model.Fingers)
{
    @Html.RadioButton("fingerList", fingers.Description)     
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
2

You don't add the properties to Body class!?

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

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

It looks to me that your Model is missing some itinialization; no setters have been called yet. So...

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

Same goes for the Hand property. It is likely null, no your foreach loop will not work.

Side note: don't pluralize your class names. It should be Finger and Toe, since you have a list of it.

Jack B
  • 391
  • 1
  • 5
  • 18