簡體   English   中英

ASP.NET MVC 父子視圖模型和實體框架

[英]ASP.NET MVC Parent-Child with ViewModel and Entity Framework

這是我的代碼。

家長:

public class Parent
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(50), MinLength(3)]
    [Display(Name = "Parent Name")]
    public string Name { get; set; }

    [Required]
    [MaxLength(50), MinLength(3)]
    [Display(Name = "Parent Surname")]
    public string Surname { get; set; }

    public List<Child> Children { get; set; } = new List<Child>();
}

孩子:

public class Child
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(50), MinLength(3)]
    [Display(Name = "Child Name")]
    public string Name { get; set; }

    [Required]
    [MaxLength(50), MinLength(3)]
    [Display(Name = "Child Surname")]
    public string Surname { get; set; }

    [Required]
    [Display(Name = "Parent")]
    public int ParentId { get; set; }

    [ForeignKey("ParentId")]
    public virtual Parent Parent { get; set; }
}

視圖模型:

public class ParentChildViewModel
{
    public Parent Parent { get; set; }
    public IEnumerable<Child> Children { get; set; }
}

Controller:

public class DemoController : Controller
{
    private readonly ApplicationDbContext _db;
    public DemoController(ApplicationDbContext db)
    {
        _db = db;
    }
    public async Task<IActionResult> Index(int id)
    {
        var parent = await _db.Parents.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id);

        var child = await _db.Children.AsNoTracking().Where(i => i.ParentId == id).ToListAsync();
        
        var model = new ParentChildViewModel
        {
            Parent = parent,
            Children = child
        };

        return View(model);
    }
}

看法:

@model TwoModels.Models.ParentChildViewModel
<h2> Demo Table Parent</h2>
<table style="border:solid 2px;">
    <tr>
        <td>
            Parent Name:
        </td>
        <td>
            @Html.DisplayFor(m => m.Parent.Name) <br />
        </td>
    </tr>
    <tr>
        <td>
            Parent Surname:
        </td>
        <td>
            @Html.DisplayFor(m => m.Parent.Surname) <br />
        </td>
    </tr>
</table>
<h2>Demo Table Child</h2>
<table style="border:solid 2px;">
    @foreach (var item in Model.Children)
    {
        <tr>
            <td>
                Child Name:
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name) <br />
            </td>
        </tr>
        <tr>
            <td>
                Child Surname:
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Surname) <br />
            </td>
        </tr>
    }
</table>

正如代碼很容易理解的那樣,我使用: _db.Parents.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id)作為父級(我不想要一個列表,而只想要一個列表)和: _db.Children.AsNoTracking().Where(i => i.ParentId == id).ToListAsync() (這是一個列表)

但是以這種方式,對數據庫的請求被發出了兩次。 所以,我想修改代碼,保持 ViewModel 的使用,但只訪問一次數據庫。 我想到了一件事:

_db.Parents.AsNoTracking().Include(c=>c.Children).Where(i => i.Id == id).ToListAsync();

如何更改我的 Controller 代碼和視圖?

您的操作可能是這樣的:

public async Task<IActionResult> Index(int id)
    {
        var item = await _db.Parents.AsNoTracking().
.Include(c=>c.Children)
.Where(i => i.Id == id).
FirstOrDefaultAsync();

           var model = new ParentChildViewModel
        {
            Parent = item,
            Children = item.Children
        };
         item.Children=null; // only if you want to use view model alone.
        return View(model);
    }

並從 Children 屬性中刪除 new List()。 它應該是這樣的:

public List<Child> Children { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM