Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web浏览器上的表未填充。Foreach循环问题?_C#_Asp.net Mvc_Entity Framework_Foreach_Webbrowser Control - Fatal编程技术网

C# Web浏览器上的表未填充。Foreach循环问题?

C# Web浏览器上的表未填充。Foreach循环问题?,c#,asp.net-mvc,entity-framework,foreach,webbrowser-control,C#,Asp.net Mvc,Entity Framework,Foreach,Webbrowser Control,问题:我希望数据库中的宠物显示在web浏览器上。我的foreach没有出错,但是宠物没有出现 怀疑:我确信这与我如何在HomeController中创建一个新列表有关,将其传递到索引中的分部,然后在_PetListView中使用@model List 请求:你能帮我理解我错过了什么吗 Pet.cs namespace.Models { 公营宠物 { [关键] 公共int-PetId{get;set;} [必需] 公共字符串名称{get;set;} [必需] 公共字符串类型{get;set;} [

问题:我希望数据库中的宠物显示在web浏览器上。我的foreach没有出错,但是宠物没有出现

怀疑:我确信这与我如何在HomeController中创建一个新列表有关,将其传递到索引中的分部,然后在_PetListView中使用@model List

请求:你能帮我理解我错过了什么吗

Pet.cs

namespace.Models
{
公营宠物
{
[关键]
公共int-PetId{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串类型{get;set;}
[必需]
公共字符串说明{get;set;}
公共字符串Skill1{get;set;}
公共字符串Skill2{get;set;}
公共字符串Skill3{get;set;}
public DateTime CreatedAt{get;set;}
public DateTime UpdatedAt{get;set;}
公共所有者{get;set;}
public int?OwnerId{get;set;}
}
}
HomeController.cs

[HttpGet(“/”)]
公共IActionResult索引()
{
列出所有宠物=新建列表();
返回视图(“索引”,所有宠物);
}
Index.cshtml

@{
List newPet=新列表();
所有者newOwner=新所有者();
}
\u PetListView.cshtml

@型号列表
宠物收容所
这些宠物正在寻找一个好家!
名称
类型
行动
@foreach(模型中的宠物动物)
{
@动物。名字
@动物型
@动物。描述
}

您正在传递空列表。你必须用宠物和主人填满它们

首先,创建一个viewModeL

public class PetViewModel
{
   public List<Pet> Pets {get; set;};
  public  List <Owner> Owners {get; set;};
}
并修复视图

Index.cshtml

@model PetViewModel



<div>
    <div id="pets">
        <partial name= "_PetListView" model= "@Model.Pets" />
     </div>

      <div id="owners">
        <partial  name ="_OwnerListView" model= "@Model.Owners" />
    </div>
</div>

您正在传递一个空列表。你必须用宠物填充它。
List allPets=new List()中没有宠物。您需要调用数据源以获取pets。@develenvonate如何调用数据库?我在数据库中有两个宠物,但我的查询不起作用。-除了我试图定义var模型外,一切都有意义。我不喜欢在PetViewModel中包含{Pets,Owners}。这只是一个示例。好的,如果我想在控制器中创建/存储实例,那么您提供的方法是有效的。但是如果我有一个已经包含实例的数据库呢?例如,我已经创建了两个宠物。我如何查询数据库,让这些宠物呈现在页面上?就是这样!好的,所以我认为我需要。包括(p=>p.Pets)。这就是我在理解上的困惑。再次谢谢你,谢尔盖!
Index.cshtml

@model PetViewModel



<div>
    <div id="pets">
        <partial name= "_PetListView" model= "@Model.Pets" />
     </div>

      <div id="owners">
        <partial  name ="_OwnerListView" model= "@Model.Owners" />
    </div>
</div>
 var model = new PetViewModel 
      {
      Pets= new List<Pet>{ new Pet {Name="PetName", Type="PetType", Description="PetDesctription"} },
      Owners = new List<Owner> { new Owner{ FirstName="FirstName", LastName="LastName"}}
       }
var model = new PetViewModel 
      {
      Pets= _context.Pets.ToList(),
      Owners = _context.Owners.ToList()
       }