我正在开展动物收容所申请。我的DAL中有一个AnimalInitializer.cs,我把它按物种划分为狗和猫。在我的控制器中,我想查看一个ActionResult的狗列表和另一个ActionResult的猫列表。当我运行ShowDogs视图时,它们都以狗身份返回,并且在ShowCats视图中没有返回任何内容。
宠物模型:
public enum SpeciesType { Dog, Cat }
public class Pet
{
public SpeciesType SpeciesType { get; set; }
}
AnimalInitializer种子:
var dogs = new List<Dog>
{
new Dog { SpeciesType = SpeciesType.Dog, Breed = "German Shepherd" }
}
var Cats = new List<Cat>
{
new Cat { SpeciesType = SpeciesType.Cat, Breed = "Domestic Long Haired" },
}
PetController:
public ActionResult ShowDogs()
{
var dogs = db.Pets.Where(d => d.SpeciesType == SpeciesType.Dog);
return View(dogs.ToList());
}
public ActionResult ShowCats()
{
var cats = db.Pets.Where(c => c.SpeciesType == SpeciesType.Cat);
return View(cats.ToList());
}
ShowDogs查看:
@model IEnumerable<AnimalShelter1.Models.Pet>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Breed)
</th>
<th>
@Html.DisplayNameFor(model => model.SpeciesType)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Breed)
</td>
<td>
@Html.DisplayFor(modelItem => item.SpeciesType)
</td>
</tr>
}
</table>
ShowCats查看:
@model IEnumerable<AnimalShelter1.Models.Pet>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.SpeciesType)
</th>
<th>
@Html.DisplayNameFor(model => model.Breed)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SpeciesType)
</td>
<td>
@Html.DisplayFor(modelItem => item.Breed)
</td>
</tr>
}
</table>
答案 0 :(得分:0)
我不确定以下解决方案是否有效。但请试试这个:
public ActionResult ShowDogs()
{
var dogs = db.Pets.Where(d => d.SpeciesType == SpeciesType.Dog).ToList();
return View(dogs);
}
public ActionResult ShowCats()
{
var cats = db.Pets.Where(c => c.SpeciesType == SpeciesType.Cat).ToList();
return View(cats);
}
答案 1 :(得分:0)
我刚刚编辑那些应该是猫并保存它们的东西,而这似乎已经奏效了。每次我启动应用程序时,动物都在正确的位置,ShowDogs和ShowCats的断点都会被正确的动物击中并填充。很奇怪,我不知道它出了什么问题...