我无法将我的LINQ SYNTAX语句应用于我的MVC视图

时间:2018-09-22 22:12:19

标签: c# linq model-view-controller

首先我使用实体框架和mvc5 我有一个类别表和一个子类别表,我在子类别表中创建了categoryid的外键,我试图编写一个查询,将子类别表中的categoryid与类别表中的categoryid进行比较,并将其放入代码中我单击一个类别,然后得到具有相同类别ID的子类别。

以下是表格:

public class Category
    {
        public Category()
        {
            SubCats = new HashSet<SubCat>();
        }

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

        [ForeignKey("SubCats")]
        public int? SubCatId { get; set; }
        public ICollection<SubCat> SubCats { get; set; }
    }



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

        [ForeignKey("Category")]
        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }

这是我在类别控制器中编写的代码:

public class CategoriesController : Controller
    {
        new ApplicationDbContext _context;

        public CategoriesController()
        {
            _context = new ApplicationDbContext();
        }

        // GET: Categories
        public ActionResult Index()
        {
            var category = _context.Categories.ToList();

            var cat = new CategoryViewModel
            {
                Categories = category
            };

            return View(cat);
        }

        public ActionResult Query()
        {
            var category = _context.Categories.ToList();
            var subcat = _context.SubCats.ToList();

            var cats = new CatSubCatViewModel
            {
                Categories = category,
                SubCats = subcat
            };

            return View(cats);
        }
    }

我认为我应该将查询操作结果放置在subcat控制器中!

这是类别索引视图:

@model Ebart.ViewModels.CategoryViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Categories</h1>

<br/>

<ul>
    @foreach (var category in Model.Categories)
    {
    <a href="http://localhost:51438/Categories/Query"><li>@category.Description</li></a>
    }
</ul>

当我单击此处的li时,我希望将我重定向到具有其类别ID的子猫

这是查询的视图:

@model Ebart.ViewModels.CatSubCatViewModel
@{
    ViewBag.Title = "Query";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Sub-Categories</h2>


@{

    var query =
         from a in Model.Categories
         from b in Model.SubCats
         where b.CategoryId == a.Id
         select b;





}

<ul>
    @foreach (var c in query.Distinct())
    {
        <li>@c.Description</li>
    }
</ul>

哦,我制作了包含类别和子类别列表的视图模型:

public class CategoryViewModel
    {
        public IEnumerable<Category> Categories { get; set; }
    }



public class CatSubCatViewModel
    {
        public IEnumerable<Category> Categories { get; set; }
        public IEnumerable<SubCat> SubCats { get; set; }
    }

最简单的解决方法是什么?

1 个答案:

答案 0 :(得分:0)

改为尝试<li>

@Html.ActionLink(c.Description, "Query", "CategoriesController, new { id= item.id })

我认为您应该将queryView移到Controller

并将Query方法设置为:

    public ActionResult Query(id)
    {
        var cat = _context.Categories
             .Include(c=>c.Subcat)
             .FirstOrDefault(c=>c.Id == id)

        return View(cat);
    }

现在您可以在目的地视图中使用f @Html.DisplayFor(modelItem => item.Description)