在Model中搜索项目

时间:2015-05-13 19:10:49

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在ASP.NET MVC中做一个小测试项目。我想在视图中搜索特定的模型项。

Testproject /控制器/的TestController

public class TestController : Controller
{
    List<FooModel> testList = new List<FooModel>() { };

    public ActionResult Index()
    {
        return View(testList);
    }
}

Testproject /型号/ FooModel.cs

public class FooModel
{
    public int Id { get; set; }
    public string TestString{ get; set; }
}

Testproject /查看/测试/ Index.cshtml

@model IEnumerable<Testproject.Models.FooModel>
<html>
<head></head>
<body>
    @Model.Find(Id = 3)
</body>
</html>

是否有某种方法可以将FooModel中Id的类型设置为键类型或其他类型,以便我可以从视图中的IEnumerable模型中搜索它?

1 个答案:

答案 0 :(得分:1)

您可以使用词典。

@model IDictionary<int, Testproject.Models.FooModel>
<html>
<head></head>
<body>
    @Model[3]
</body>
</html>
相关问题