ASP.NET Core 6 MVC / C#:从多方获取一对多的数据

huangapple go评论66阅读模式
英文:

ASP.NET Core 6 MVC / C# : one-to-many get data from many side

问题

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with many posts. I just want to list the blogs with their posts in the view.

For example...

Blog1
    post1
    post2
    post3
Blog2
    post1
    post2

That's pretty much it. But as it stands, in the view, I am unable to access the post data.

Here are my models with the relevant properties

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Post>? Posts { get; set; } = new HashSet<Post>();
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int BlogId { get; set; }
}

And here is the controller

public async Task<IActionResult> Index()
{
    var applicationDbContext = _context.Blogs
            .Include(b => b.BlogUsers)
            .Include(p => p.Posts)
            .ToListAsync();

    return View(await applicationDbContext);
}

I've included the Posts so I should be able to access the Post data. I can access the Blogs data no problem.

Any ideas?

英文:

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with many posts. I just want to list the blogs with their posts in the view.

For example...

Blog1
    post1
    post2
    post3
Blog2
    post1
    post2

That's pretty much it. But as it stands, in the view, I am unable to access the post data.

Here are my models with the relevant properties

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection&lt;Post&gt;? Posts { get; set; } = new HashSet&lt;Post&gt;();
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int BlogId { get; set; }
}

And here is the controller

public async Task&lt;IActionResult&gt; Index()
{
    var applicationDbContext = _context.Blogs
            .Include(b =&gt; b.BlogUsers)
            .Include(p =&gt; p.Posts)
            .ToListAsync();

    return View(await applicationDbContext);
}

I've included the Posts so I should be able to access the Post data. I can access the Blogs data no problem.

Any ideas?

答案1

得分: 1

Here's the translated code:

如果您想获取帖子,可以尝试:

public async Task<IActionResult> Index()
{
     var applicationDbContext = await _context.Blogs
      .Include(b => b.BlogUsers)
      .Include(p => p.Posts)
      .ToListAsync();
    return View(applicationDbContext);
}

如果您想使用`@item.Posts.`,属性将在点操作后出现在智能感知中。您需要使用:

public virtual Post? Posts { get; set; }

因为`ICollection<Post>`没有Id的定义。我们在Post中定义了Id

> 例如,我希望`@item.Posts.Title`能够工作,

请尝试:

@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)                 
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
       @* @item.Posts.Id*@
            @foreach (var item2 in item.Posts)
            {
                 @item2.Title
            }
    }

I've translated the code for you.

英文:

If you want to get Posts you can try:

public async Task&lt;IActionResult&gt; Index()
        {
             var applicationDbContext = await _context.Blogs
              .Include(b =&gt; b.BlogUsers)
              .Include(p =&gt; p.Posts)
              .ToListAsync();
            return View(applicationDbContext);
        }

If you want @item.Posts., the properties appear in the intellisence after the dot. You need to use

public virtual Post? Posts { get; set; }

Because ICollection&lt;Post&gt; don't have a definition for Id. We define the Id in the Post .

> For example I was hoping that @item.Posts.Title would work,

Try:

@foreach (var item in Model) {
        &lt;tr&gt;
            &lt;td&gt;
                @Html.DisplayFor(modelItem =&gt; item.Name)                 
            &lt;/td&gt;
            &lt;td&gt;
                &lt;a asp-action=&quot;Edit&quot; asp-route-id=&quot;@item.Id&quot;&gt;Edit&lt;/a&gt; |
                &lt;a asp-action=&quot;Details&quot; asp-route-id=&quot;@item.Id&quot;&gt;Details&lt;/a&gt; |
                &lt;a asp-action=&quot;Delete&quot; asp-route-id=&quot;@item.Id&quot;&gt;Delete&lt;/a&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
       @* @item.Posts.Id*@
            @foreach (var item2 in item.Posts)
            {
                 @item2.Title
            }
        }

huangapple
  • 本文由 发表于 2023年5月30日 07:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定