locked
Show in my dropdown list only the Partners with the goog user RRS feed

  • Question

  • User1991482975 posted

    Hi,

    i generate a SelectList

     id = USER I CHOOSE
    
    var PartnerData = db.Partner
     .Where(m => m.ListPartnerUser. ( UserId taken from my PartnerUsers table) != id)
                    .Select(m => new
                   {                   
                       PartnerId = m.PartnerId,
                       PartnerTitleAndLangue = m.PartnerTitle + " (" + m.PartnerLangue + ")"
                   })
                   .ToList();
                ViewBag.PartnerId = new SelectList(PartnerData, "PartnerId", "PartnerTitleAndLangue");

    which i use to populate my dropdownlist

    @Html.DropDownList("partnerid", ViewBag.PartnerId as List<SelectListItem>, ViewRes.Shared.PleaseSelect, new { @class = "wid100x100" })

    I have thos two models

     
    public class Partner
        {
            public int PartnerNameId { get; set; }
            public virtual PartnerName ParentPartnerName { get; set; }
    
            [Key]
            public int PartnerId { get; set; }
    
            [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
            [Display(Name = "PartnerLangue", ResourceType = typeof(ViewRes.NamesPartner))]
            public string PartnerLangue { get; set; }
    
            [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
            [Display(Name = "PartnerTitle", ResourceType = typeof(ViewRes.NamesPartner))]
            [StringLength(2000)]
            [AllowHtml]
            public string PartnerTitle { get; set; }
    }
        }
    
    
    public class PartnerUser
        {
            public int PartnerId { get; set; }
            public virtual Partner ParentPartnerForUser { get; set; }
    
            [Display(Name = "UserId", ResourceType = typeof(ViewRes.NamesPartner))]
            public string UserId { get; set; }
            //[ForeignKey("UserID")]
            //public virtual ApplicationUser User { get; set; }
    
            [Key]
            public int PartnerUserId { get; set; }
    
    
        }

    and i need to show only the partners that HAVE NOT already the user saved for that partner in the  PartnerUsers

    How to modify my code?

    Thursday, December 17, 2020 10:12 AM

Answers

  • User1991482975 posted

    Here the solution!

    var existingIds = db.PartnerUsers.Where(m => m.UserId == id).Select(x => x.PartnerId).ToList();
                var exceptPartner = db.Partners.Where(x => !existingIds.Contains(x.PartnerId));
                if (exceptPartner != null)
                {
                    var PartnerData = exceptPartner.Select(m => new
                    {
                        PartnerId = m.PartnerId,
                        PartnerTitleAndLangue = m.PartnerTitle + " (" + m.PartnerLangue + ")"
                    });
                    ViewBag.PartnerId = new SelectList(PartnerData, "PartnerId", "PartnerTitleAndLangue");
    
                    ViewBag.Message = id;
                }
                else
                {
                    ViewBag.PartnerId = null;
                    ViewBag.Message = ViewRes.NamesPartner.PartnerAvailableNo;
                }

    i finally Find it ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 18, 2020 2:04 PM

All replies

  • User1686398519 posted

    Hi grafic.web, 

    grafic.web

    i need to show only the partners that HAVE NOT already the user saved for that partner in the  PartnerUsers

    Do you want to get all the partners that the user(for example, UserId="1")does not have?

    If this is the case, you can refer to the following code.

    If this is not the case, can you describe your problem in more detail?

    Model

        public class Partner
        {
            [Key]
            public int PartnerId { get; set; }
            public string PartnerTitle { get; set; }
            public string PartnerLangue { get; set; }
            public virtual ICollection<PartnerUser> ListPartnerUser { get; set; }
        }
        public class PartnerUser
        {
            [Display(Name = "UserId")]
            public string UserId { get; set; }
            [Key]
            public int PartnerUserId { get; set; }
            public virtual ICollection<Partner> ListPartnerForUser { get; set; }
        }

    Controller

            [HttpPost]
            public ActionResult Index(string UserId)
            {
                var allpartners = db.Partners.ToList();
                var partnerUsersWithUserId = db.PartnerUsers.Include("ListPartnerForUser")
                   .Where(m => m.UserId == UserId)
                   .Select(m => m.ListPartnerForUser.ToList()).FirstOrDefault();
                var exceptPartner = partnerUsersWithUserId != null
                    ?allpartners.Except(partnerUsersWithUserId).ToList():null;
                if(exceptPartner != null)
                {
                    var PartnerData = exceptPartner.Select(m => new
                    {
                        PartnerId = m.PartnerId,
                        PartnerTitleAndLangue = m.PartnerTitle + " (" + m.PartnerLangue + ")"
                    });
                    ViewBag.PartnerId = new SelectList(PartnerData, "PartnerId", "PartnerTitleAndLangue");
    
                    ViewBag.Message = UserId;
                }
                else
                {
                    ViewBag.PartnerId = null; 
                    ViewBag.Message = "NO Partner";
                }
                return View();
            }

    View

    @using (Html.BeginForm("Index", "Test99", FormMethod.Post))
    {
        <div class="panel panel-primary">
            <div class="panel-body">
                Test UserId:<input name="UserId" class="form-control" />
                <button type="submit">submit</button>
                @if (ViewBag.PartnerId != null)
                {
                    @Html.DropDownList("partnerid", ViewBag.PartnerId as List<SelectListItem>, "PleaseSelect", new { @class = "wid100x100" })
                }
                @ViewBag.Message
            </div>
        </div>
    }

    Here is the result.

    Best Regards,

    YihuiSun

    Friday, December 18, 2020 9:46 AM
  • User1991482975 posted

    Hi,

    i am lost...

    i do not get your code..

    why you take off the partnerid from my table PartnerUsers?

    public class PartnerUser
        {
            [Display(Name = "UserId")]
            public string UserId { get; set; }
            [Key]
            public int PartnerUserId { get; set; }
            public virtual ICollection<Partner> ListPartnerForUser { get; set; }
        }

    In this table i add one or more user for each partner i want..

    So let say i wanto to add Carlos to the partner 1 and 2

    I add in my PartnerUsers table

    Userid: Carlos , Partnerid : 1

    Userid: Carlos , Partnerid : 2

    So i need to show in the list of partners only the parners 3 and 4 because carlos is not added in the PartnerUsers table for the partners 3 and 4.

    I hope i explained better what i need to achive

    Friday, December 18, 2020 11:02 AM
  • User1991482975 posted

    Hi,

    i am almost there... i modify with this code :

    model :

    public class Partner
        {
            public int PartnerNameId { get; set; }
            public virtual PartnerName ParentPartnerName { get; set; }
    
            [Key]
            public int PartnerId { get; set; }
    
            [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
            [Display(Name = "PartnerLangue", ResourceType = typeof(ViewRes.NamesPartner))]
            public string PartnerLangue { get; set; }
    
            [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
            [Display(Name = "PartnerTitle", ResourceType = typeof(ViewRes.NamesPartner))]
            [StringLength(2000)]
            [AllowHtml]
            public string PartnerTitle { get; set; }
    }
        }
    
    
    public class PartnerUser
        {
            public int PartnerId { get; set; }
            public virtual Partner ParentPartnerForUser { get; set; }
    
            [Display(Name = "UserId", ResourceType = typeof(ViewRes.NamesPartner))]
            public string UserId { get; set; }
            //[ForeignKey("UserID")]
            //public virtual ApplicationUser User { get; set; }
    
            [Key]
            public int PartnerUserId { get; set; }
    
    
        }

    controller

    var allpartners = db.PartnerUsers.Where(m => m.UserId != id).ToList();
                var partnerUsersWithUserId = db.Partners.Include("ListPartnerUser")               
                   .Select(m => m.ListPartnerUser.ToList()).FirstOrDefault();
                var exceptPartner = partnerUsersWithUserId != null
                    ? allpartners.Except(partnerUsersWithUserId).ToList() : null;
                if (exceptPartner != null)
                {
                    var PartnerData = exceptPartner.Select(m => new
                    {
                        PartnerId = m.PartnerId,
                        PartnerTitleAndLangue = m.ParentPartnerForUser.PartnerTitle + " (" + m.ParentPartnerForUser.PartnerLangue + ")" + " (" + m.PartnerUserId + ")"
                    });
                    ViewBag.PartnerId = new SelectList(PartnerData, "PartnerId", "PartnerTitleAndLangue");
    
                    ViewBag.Message = id;
                }
                else
                {
                    ViewBag.PartnerId = null;
                    ViewBag.Message = ViewRes.NamesPartner.PartnerAvailableNo;
                }

    the problem is that i have to make a group by with the partnerid when i try to avoid the partner of the user, cause others users can have been added for the partner and so i will see it on my list anyway..

    i explain better:

    Users : Carlos, Kamil, Franco

    Carlos added for partner A, B, C, E

    Kamila added for partner  B, C,

    Francoadded for partner C, D, E

    So if i want to show the partners non already given to Carlos, with this code i will still show B C E, cause are added for the 2 other users and this code check the PArnterUSers Table.. so how to modify my code?

    I hope i explained well

    Friday, December 18, 2020 12:55 PM
  • User1991482975 posted

    Here the solution!

    var existingIds = db.PartnerUsers.Where(m => m.UserId == id).Select(x => x.PartnerId).ToList();
                var exceptPartner = db.Partners.Where(x => !existingIds.Contains(x.PartnerId));
                if (exceptPartner != null)
                {
                    var PartnerData = exceptPartner.Select(m => new
                    {
                        PartnerId = m.PartnerId,
                        PartnerTitleAndLangue = m.PartnerTitle + " (" + m.PartnerLangue + ")"
                    });
                    ViewBag.PartnerId = new SelectList(PartnerData, "PartnerId", "PartnerTitleAndLangue");
    
                    ViewBag.Message = id;
                }
                else
                {
                    ViewBag.PartnerId = null;
                    ViewBag.Message = ViewRes.NamesPartner.PartnerAvailableNo;
                }

    i finally Find it ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 18, 2020 2:04 PM