Displaying Text from Joined Property Sets in Join Property Set Edit View

Dean Everhart 1,536 Reputation points
2023-04-24T18:18:11.13+00:00

Two Property Sets ("Item" and "Reference") with a Join Table ("Attribution") between them. Context

        public DbSet<Note.Models.Item>? Item { get; set; }

        public DbSet<Note.Models.Reference>? Reference { get; set; }

        public DbSet<Note.Models.Attribution>? Attribution { get; set; }

The join table ("Attribution") displays the two Primary IDs for the join in the edit view. I would like this view to display more descriptive fields in addition to the IDs. User's image

When I add in code to display a property ("Text") from the Item property set, it doesn't accept the code (underlined in red below). User's image

Question: How do I format the code to display fields from either of the two joined tables ("Item" and "Reference"). Item and Reference Models

Item

using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace Note.Models
{
    public class Item
    {
        public int Id { get; set; }

        public string? Text { get; set; }

        public string? Type { get; set; }

        [Display(Name = "TextAbstract")]
        public string TextAbstract
        {
            get { return Text + ", " + Abstract; }
        }

        public string? Subject { get; set; }

        public string? Topic { get; set; }

        public string? Abstract { get; set; }

        public string? KeyWords { get; set; }

        public string? Tag { get; set; }


        // ____________ Language ______________

        public string? Language { get; set; }

        public string? Version { get; set; }

        // __________ Language (end) ______________


        [Display(Name = "Created")]
        [DataType(DataType.Date)]                       // [DataType(DataType.Date)] = only date, no time information
        public DateTime? DateCreate { get; set; }


        // ____________ Flash Card ________________

        public string? Text1a { get; set; }

        public string? Text1b { get; set; }

        public string? Text1c { get; set; }

        public string? Text1d { get; set; }

        public string? Text2a { get; set; }

        public string? Text2b { get; set; }

        public string? Text2c { get; set; }

        public string? Text2d { get; set; }

        // _________ Flash Card (end) _____________


        // ____________ Reference ________________

        public string? Ref1 { get; set; }

        public string? Ref1Access { get; set; }

        public string? Ref2 { get; set; }

        public string? Ref2Access { get; set; }

        // _________ Reference (end) ________________


        // _______________________________________

        public ICollection<Attribution>? Attribution { get; set; }

    }
}

Reference

using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace Note.Models
{
    public class Reference
    {
        public int Id { get; set; }

        public string? Heading { get; set; }

        [Display(Name = "HeadingSource")]
        public string HeadingSource
        {
            get { return Heading + ", " + Source; }
        }

        public string? HeadingAccess { get; set; }

        public int? HeadingNumber { get; set; }

        public string? Source { get; set; }

        public string? Type { get; set; }

        public string? WebPage { get; set; }

        public string? WebPageAccess { get; set; }

        public DateTime? DateCreate { get; set; }

        // __________ Text Book ________________

        public int? SectionNumber { get; set; }

        public string? Section { get; set; }

        public int? ChapterNumber { get; set; }

        public string? Chapter { get; set; }

        public int? PageNumber { get; set; }

        public int? ParagraphNumber { get; set; }

        public DateTime? DatePublished { get; set; }

        // __________ Text Book (end) ____________


        // _______________________________________

        public ICollection<Attribution>? Attribution { get; set; }
    }
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,667 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,536 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,551 Reputation points Microsoft Vendor
    2023-04-25T06:28:18.0733333+00:00

    Hi @Dean Everhart

    According to your code, the Item and Reference table contains many to many relationships, the join table class should like this:

    public class Attribution
    {
        public int ItemId { get; set; } 
        public Item Item{ get; set; }  
    
        public int ReferenceId { get; set; }
        public Reference Reference { get; set; }
    }
    

    The join table ("Attribution") displays the two Primary IDs for the join in the edit view. I would like this view to display more descriptive fields in addition to the IDs.

    For the join table ("Attribution"), if you want to add additional property, you should add it in the Attribution class, like this:

    public class Attribution
    {
    
        public int ItemId { get; set; } 
        public Item Item{ get; set; }    //navigation property
    
        public int ReferenceId { get; set; }
        public Reference Reference { get; set; }   //navigation property
    
        public string Text {get; set;} //additional property in the join table.
    }
    

    Then, in the view page, you can display the Text property directly using asp-for ="Text", as below:
    User's image

    How do I format the code to display fields from either of the two joined tables ("Item" and "Reference")

    If you want to display the Item or Reference's fields via the Attribution, you should use the navigation property in the Attribution class, code like this:

    @model Note.Models.Attribution
    ...
     
                <div class="form-group">
                    <label asp-for="Item.Text" class="control-label"></label>
                    <input asp-for="Item.Text" class="form-control" />
                    <span asp-validation-for="Item.Text" class="text-danger"></span>
                </div>  
    
    

    More detail information about Many-to-many relationships, see:
    Many-to-many relationships
    EF Core Many To Many Relationship


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.