tsh*_*ake 4 .net c# asp.net-mvc razor asp.net-mvc-4
我想为mvc页面制作一个这样的模型:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后有一个像这样的手指类:
public class Fingers
{
public int Id { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在我的视图中像这样访问它:
@model Models.Body
@foreach (var fingers in Model.Hand.Fingers)
{
@Html.RadioButton("fingerList", fingers.Description)
}
Run Code Online (Sandbox Code Playgroud)
我的模特错了吗?现在VS并不认识Model.Hand到foreach,更不用说了Model.Hand.Fingers.我不想做@model IEnumerable,因为这个页面只显示一个person,但它可以有多个列表fingers,toes等等.
你的Body班级没有财产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; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后你需要引用与你的属性匹配的外壳属性,而不是类名:
@model Models.Body
@foreach (var fingers in Model.hand.fingers)
{
@Html.RadioButton("fingerList", fingers.Description)
}
Run Code Online (Sandbox Code Playgroud)
我还认为你的套管/大写是不正确的,但这不是你的问题的重点.