English 中文(简体)
asp.net 6 c# mvc 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?

问题回答

If you want to get Posts you can try:

public async Task<IActionResult> Index()
        {
             var applicationDbContext = await _context.Blogs
              .Include(b => b.BlogUsers)
              .Include(p => 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<Post> 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) {
        <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
            }
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签