locked
How to return the same child List with different condition in the same parent? RRS feed

  • Question

  • User991566988 posted

    Hi

    I'm calling tow partial views of the same model at a parent one, But I want to call every one with a different condition of the other.

    The parent Model:

        public class Mobiles
        {
            public int Id { get; set; }
    
            public string SerialNo { get; set; }
    
            public string Notes { get; set; }
    
            public IList<MobileRuins> MobileRuins { get; set; } = new List<MobileRuins>();
    public IList<MobileRuins> MobileRuins2 { get; set; } = new List<MobileRuins>(); }

    The child MobileRuins class: 

        public class MobileRuins
        {
            public int Id { get; set; }
            
            public int RuinId { get; set; }
    
            public int MobileId { get; set; }
    
            public string RuinDetails { get; set; }
    
            public string Notes { get; set; }
    
            public Mobiles Mobile { get; set; }
            
            public Ruins Ruin { get; set; }
    
        }

    Including the tow lists in the MobilesController:

        public class MobilesController : Controller
        {
            private readonly ApplicationDbContext _context;
    
            public MobilesController(ApplicationDbContext context)
            {
                _context = context;
            }
            public async Task<IActionResult> Edit(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                var mobiles = await _context.Mobiles
                    .Include(n => n.MobileRuins)
                    .ThenInclude(m => m.Ruin)
                    .Include(n => n.MobileRuins2)
                    .ThenInclude(m => m.Ruin)
                    .FirstOrDefaultAsync(n => n.Id == id.Value); 
                     mobiles.MobileRuins= mobiles.MobileRuins.Where(c => c.RuinId == 1).ToList();
                     mobiles.MobileRuins2= mobiles.MobileRuins2.Where(c => c.RuinId == 2).ToList();
                if (mobiles == null)
                {
                    return NotFound();
                }
                ViewData["RuinId"] = new SelectList(_context.Ruins, "Id", "Ruin");
    
                return View(mobiles);
            }

    The tow partail views ParMobRui and ParMobRui2 with the same model:

    @model Mobiles

    Calling the tow partial views at Mobiles/Edit view:

                <div class="tab-content">
                    <div id="home" class="tab-pane fade in active">
                        @{ await Html.RenderPartialAsync("~/Views/MobileRuins/ParMobRui.cshtml", Model); }
                    </div>
                    <div id="home" class="tab-pane fade">
                        @{ await Html.RenderPartialAsync("~/Views/MobileRuins/ParMobRui2.cshtml", Model); }
                    </div>
                </div>

    Now this scenario gives me this error:

    InvalidOperationException: Unable to determine the relationship represented by navigation property 'MobileRuins.Mobiles' of type 'Mobiles'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

    When trying to put [NotMapped] like this:

    [NotMapped]
    public IList<MobileRuins> MobileRuins2 { get; set; } = new List<MobileRuins>();

    This gives me this error:

    NullReferenceException: Object reference not set to an instance of an object.

    How to solve please?

    Tuesday, October 20, 2020 9:25 AM

Answers

  • User1312693872 posted

    Hi,musbah7@hotmail.com

    InvalidOperationException: Unable to determine the relationship represented by navigation property 'MobileRuins.Mobiles' of type 'Mobiles'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

    No need to use [NotMapped], the problem is in your navigation properties, you should add two foreign keys while there are two lists in 'Mobiles':

    Model:

     public class MobileRuins
        {
            public int Id { get; set; }
            public int RuinId { get; set; }
            public string RuinDetails { get; set; }
            public string Notes { get; set; }
    
            public int MobileId_1 { get; set; }
            public Mobiles Mobile { get; set; }
            public int MobileId_2 { get; set; }
            public Mobiles Mobile2 { get; set; }
           ....
    }

    DbContext to define the foreign key:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Mobiles>().ToTable("Mobiles");
                modelBuilder.Entity<MobileRuins>()
                .HasOne(f => f.Mobile)
                .WithMany(f => f.MobileRuins)
                .HasForeignKey(g => g.MobileId_1);
    
                modelBuilder.Entity<MobileRuins>()
                .HasOne(f => f.Mobile2)
                .WithMany(f => f.MobileRuins2)
                .HasForeignKey(g => g.MobileId_2);
            }

    Then the problem will gone.

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 6:52 AM

All replies

  • User475983607 posted

    Do you actually have a MobileRuins2 entity?

                var mobiles = await _context.Mobiles
                    .Include(n => n.MobileRuins)
                    .ThenInclude(m => m.Ruin)
                    .Include(n => n.MobileRuins2)
                    .ThenInclude(m => m.Ruin)
                    .FirstOrDefaultAsync(n => n.Id == id.Value); 
                     mobiles.MobileRuins= mobiles.MobileRuins.Where(c => c.RuinId == 1).ToList();
                     mobiles.MobileRuins2= mobiles.MobileRuins.Where(c => c.RuinId == 2).ToList();

    Always share the line of code that causes the error when you ask for support on the forums.

    Tuesday, October 20, 2020 12:22 PM
  • User1312693872 posted

    Hi,musbah7@hotmail.com

    InvalidOperationException: Unable to determine the relationship represented by navigation property 'MobileRuins.Mobiles' of type 'Mobiles'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

    No need to use [NotMapped], the problem is in your navigation properties, you should add two foreign keys while there are two lists in 'Mobiles':

    Model:

     public class MobileRuins
        {
            public int Id { get; set; }
            public int RuinId { get; set; }
            public string RuinDetails { get; set; }
            public string Notes { get; set; }
    
            public int MobileId_1 { get; set; }
            public Mobiles Mobile { get; set; }
            public int MobileId_2 { get; set; }
            public Mobiles Mobile2 { get; set; }
           ....
    }

    DbContext to define the foreign key:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Mobiles>().ToTable("Mobiles");
                modelBuilder.Entity<MobileRuins>()
                .HasOne(f => f.Mobile)
                .WithMany(f => f.MobileRuins)
                .HasForeignKey(g => g.MobileId_1);
    
                modelBuilder.Entity<MobileRuins>()
                .HasOne(f => f.Mobile2)
                .WithMany(f => f.MobileRuins2)
                .HasForeignKey(g => g.MobileId_2);
            }

    Then the problem will gone.

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 6:52 AM
  • User991566988 posted

    You are right

    Thanks

    Friday, October 23, 2020 8:49 AM