Skip to content

[Netcore] 复杂数据模型

Jinxin Chen edited this page Dec 11, 2019 · 1 revision

外键约定

public class Article
{
    public string CategoryId { get; set; }
    public Category Category { get; set; }
}

利用ViewData传递

    ViewData["CategoryId"] = new SelectList(_dbContext.Category.ToList(), "Id", "CategoryName");
    <select asp-for="CategoryId" class="form-control" asp-items="ViewBag.CategoryId"></select>

返回相关数据

    var article = await _dbContext.Article.Include(c => c.Category).SingleOrDefaultAsync(m => m.Id == key);
Clone this wiki locally