Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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/9/git/21.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
Asp.net mvc MVC ActionResult不是';不要正确地抓住表达式的位置_Asp.net Mvc_Actionresult - Fatal编程技术网

Asp.net mvc MVC ActionResult不是';不要正确地抓住表达式的位置

Asp.net mvc MVC ActionResult不是';不要正确地抓住表达式的位置,asp.net-mvc,actionresult,Asp.net Mvc,Actionresult,我在做动物收容所的申请。我的DAL中有一个AnimalInitializer.cs,我将它按物种分解为狗和猫。在我的控制器中,我想查看一个ActionResult的狗列表和另一个ActionResult的猫列表。当我运行ShowDogs视图时,它们都以狗的身份返回,而ShowCats视图上没有返回任何内容 宠物型号: public enum SpeciesType { Dog, Cat } public class Pet { public SpeciesType SpeciesTy

我在做动物收容所的申请。我的DAL中有一个AnimalInitializer.cs,我将它按物种分解为狗和猫。在我的控制器中,我想查看一个ActionResult的狗列表和另一个ActionResult的猫列表。当我运行ShowDogs视图时,它们都以狗的身份返回,而ShowCats视图上没有返回任何内容

宠物型号:

public enum SpeciesType { Dog, Cat }

public class Pet 
{
    public SpeciesType SpeciesType { get; set; }
}
动物初始化剂种子:

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" },
}
展品视图:

@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>
@model IEnumerable


我不确定下面的解决方案是否有效。但请尝试一下:

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

我只是编辑了那些本来应该是猫的,然后救了它们,这似乎奏效了。每次我启动应用程序时,动物都在正确的位置,ShowDogs和ShowCats的断点都会被正确的动物击中并填充。奇怪,我不知道出了什么问题…

ShowDogs
ShowCats
视图几乎相同,而
ShowCats
不返回任何结果吗?在您当前的示例中,我看不出任何错误,请提供更多详细信息。是的,两个视图是相同的,我更新了我的问题以反映这些视图。程序运行的屏幕截图是否有用?您可以提供两个视图的屏幕截图作为问题的证明。由于查询代码和视图代码几乎相同,
ShowCats
还应返回来自
SpeciesType.Cat
条件的迭代结果集,问题可能发生在控制器或视图之外的其他地方。您确定数据库中有Cat吗???另外,您是否可以调试并查看是否在actionresult中获得cat对象???嗯。。。我看到了你的截图,发现所有
“国产长毛狗”
条目都有
SpeciesType.Dog
。您是否确实有任何表项具有
SpeciesType.Cat
?很遗憾,当我提出您的更改建议时,没有任何更改。不过,谢谢你的建议!好的,那么请在ShowCats()操作中保留断点,然后检查“var cats”是否正在接收值。如果var cats中存在数据,则您的视图有问题,否则请检查数据库中是否存在类别为cat的数据。
@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>
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);
}