gpt4 book ai didi

c# - Foreach 找不到嵌套类

转载 作者:太空狗 更新时间:2023-10-30 00:04:39 27 4
gpt4 key购买 nike

我想为这样的 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; }
}
}

然后像这样为 Fingers 创建一个类:

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

然后在我看来像这样访问它:

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

我的模型做错了吗?现在 VS 无法识别 foreach 中的 Model.Hand,更不用说 Model.Hand.Fingers 了。我不想让 @model IEnumerable 因为这个页面应该只显示一个 person,但它可以有多个列表 手指脚趾

最佳答案

您的 Body 类没有属性 Hand

除此之外,您的 Hand 类有 fingers,但在 razor 中您用 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)
}

我还认为您的大小写/大写不正确,但这不是您问题的重点。

关于c# - Foreach 找不到嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com