Compare 2 lists and select checkboxes based on Model - list

I have a dropdown list on my for populated with a list of US States populated from a lookup table and binded to a dropdown list. When my users create a new "Offer" the selected ID's of the States are stored in a Join table holding the OfferID and the corresponding StateID. I have this part working correctly, however, when I want to edit the offer and Select the various States that are stored as my "TargetStates" selected when creating the offer, I am having trouble comparing my TargetStates against the list of States used to populate all the States in the Dropdownlist.
I am using JQuery's mulitselect dropdown list in my form. I have 2 lists. One from my ViewModel EditOfferViewModel, and the other from my Offer Model with a list of States. How can I compare these two lists and select the populated DDL when the StateID in my ViewModel matches the SelectListItemID in the StateList?
Any help would be greatly Appreciated!!
Here is my Offer Model:
public partial class Offer : BaseModel
{
public Offer()
{
this.OfferTargetInviteStatus = new HashSet<OfferTargetInviteStatu>();
this.OfferTargetStates = new HashSet<OfferTargetState>();
}
[DataMember]
public long ID { get; set; }
[DataMember]
public string ProgramIdentifier { get; set; }
[DataMember]
public string PackageIdentifier { get; set; }
[DataMember]
public long LocationID { get; set; }
[DataMember]
public string DisplayName { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public decimal Price { get; set; }
[DataMember]
public string TargetGender { get; set; }
[DataMember]
public System.DateTime TargetAgeStartDate { get; set; }
[DataMember]
public System.DateTime TargetAgeEndDate { get; set; }
[DataMember]
public int ApplicantTypeID { get; set; }
[DataMember]
public bool IsActive { get; set; }
[DataMember]
public string CreatedBy { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> LastModifiedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> ArchiveDate { get; set; }
[DataMember]
public Nullable<System.DateTime> ExpiryDate { get; set; }
[DataMember]
public Nullable<long> OfferPhotoID { get; set; }
[DataMember]
public int OfferNotificationTypeID { get; set; }
[DataMember]
public string OfferBulletPoints { get; set; }
[DataMember]
public virtual ApplicantType ApplicantType { get; set; }
[DataMember]
public virtual Location Location { get; set; }
[DataMember]
public virtual NotificationType NotificationType { get; set; }
[DataMember]
public virtual OfferPhotograph OfferPhotograph { get; set; }
[DataMember]
public virtual ICollection<OfferTargetInviteStatu> OfferTargetInviteStatus { get; set; }
[DataMember]
public virtual ICollection<OfferTargetState> OfferTargetStates { get; set; }
public SelectList StateList { get; set; }
public SelectList StatusList { get; set; }
And here is my OfferTargetStates model:
public partial class OfferTargetState
{
[DataMember]
public long ID { get; set; }
[DataMember]
public long StateID { get; set; }
[DataMember]
public long OfferID { get; set; }
[DataMember]
public virtual State State { get; set; }
[DataMember]
public virtual Offer Offer { get; set; }
}
My EditOfferViewModel:
public class EditOfferViewModel
{
[DataMember]
public long Id { get; set; }
[DataMember]
public string SelectedProgramIdentifier { get; set; }
[DataMember]
public string SelectedPackageIdentifier { get; set; }
[DataMember]
public string DisplayName { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public decimal Price { get; set; }
[DataMember]
public string SelectedTargetGender { get; set; }
[DataMember]
public System.DateTime TargetAgeStartDate { get; set; }
[DataMember]
public System.DateTime TargetAgeEndDate { get; set; }
[DataMember]
public int ApplicantTypeID { get; set; }
[DataMember]
public string OfferBulletPoints { get; set; }
[DataMember]
public Nullable<System.DateTime> LastModifiedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> ArchiveDate { get; set; }
[DataMember]
public Nullable<long> OfferPhotoID { get; set; }
[DataMember]
public virtual OfferPhotograph OfferPhotograph { get; set; }
[DataMember]
public virtual ICollection<OfferTargetState> SelectedOfferTargetStates { get; set; }
[DataMember]
public virtual ICollection<OfferTargetInviteStatu> SelectedOfferTargetInviteStatus { get; set; }
[DataMember]
public virtual ICollection<Location> Locations { get; set; }
[DataMember]
public virtual ICollection<State> States { get; set; }
[DataMember]
public SelectList StateList { get; set; }
[DataMember]
public SelectList StatusList { get; set; }
[DataMember]
public SelectList PackageList { get; set; }
[DataMember]
public List<SelectListItem> OfferProgramList { get; set; }
[DataMember]
public List<SelectListItem> OfferPackageList { get; set; }
}
An example of my States Table used to popluate my checkbox DDL:
and an example of my TargetStates Join table:
In short, I create a StateList of All States, and I need to compare my SelectedOfferTarget States against that list and select the ID's that match.
UI Example:
Code to populate State List DDL:
Thanks

Related

EF Core Sort a list by related data

I implemented objects like below in ASP.NET Core 3.1:
public class Content: BaseModel
{
public int ContentId { get; set; }
public virtual List<ContentCBlock> ContentCBlocks { get; set; } = new List<ContentCBlock>();
}
public class CBlock : BaseModel
{
public int CBlockId { get; set; }
public virtual List<ContentCBlock> ContentCBlocks { get; set; } = new List<ContentCBlock>();
}
public class ContentCBlock
{
[Key]
public int ContentId { get; set; }
public virtual Content Content { get; set; }
[Key]
public int CBlockId { get; set; }
public virtual CBlock CBlock { get; set; }
public int DisplayOrder { get; set; }
}
and DBContext
modelBuilder.Entity<ContentCBlock>().HasKey(t => new { t.ContentId, t.CBlockId });
modelBuilder.Entity<ContentCBlock>()
.HasOne(c => c.CBlock)
.WithMany(c => c.ContentCBlocks)
.HasForeignKey(cc => cc.CBlockId);
modelBuilder.Entity<ContentCBlock>()
.HasOne(c => c.Content)
.WithMany(c => c.ContentCBlocks)
.HasForeignKey(cc => cc.ContentId);
DBSets:
public DbSet<ContentCBlock> ContentCBlocks { get; set; }
public DbSet<Content> Contents { get; set; }
public DbSet<CBlock> CBlocks { get; set; }
Everything works fine, relations, lazyLoading, ... everything.
The question is How I can sort a List of Contents by ContentCBlock.DisplayOrder
P.S.: I cannot change classes
Hope this will help you.
_context.Contents.OrderBy(c => c.ContentCBlocks.
OrderBy(c => c.DisplayOrder).Select(c => c.DisplayOrder).FirstOrDefault());

EF Core - Fluent API many-to-many self reference

I am attempting to configure 2 classes fluently.
public class Company
{
[Key]
public int Id {get; set; }
public string Name { get; set; }
public List<CompanyOwnership> OwnedBy { get; set; }
}
public class CompanyOwnership
{
public static void Configure(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CompanyOwnership>()
.HasOne(cpo => cpo.OwnedCompany)
.WithMany(cp => cp.OwnedBy)
.HasForeignKey(cpo => cpo.OwnedCompanyId);
modelBuilder.Entity<CompanyOwnership>()
.HasOne(cpo => cpo.OwningCompany)
.WithMany()
.HasForeignKey(cpo => cpo.OwningCompanyId);
}
[Key]
public int Id {get; set; }
public int OwnedCompanyId { get; set; }
public Company OwnedCompany { get; set; }
public int OwningCompanyId { get; set; }
public Company OwningCompany { get; set; }
public decimal Percentage { get; set; }
}
The above code will result in an error:
InvalidOperationException: Unable to determine the relationship
represented by navigation property 'Company.OwnedBy' of type
'List<CompanyOwnership>'. Either manually configure the relationship,
or ignore this property using the '[NotMapped]' attribute or by using
'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Could I get some input about why the above setup is not enough?
Thank you,
Nvm,
it turned out that I forgot to call Configure(...).
It is working fine now.

Model Causing scaffolding to crash

For some reason the following model is causing the scaffolding templates to crash it is not giving me an error just the visual studio 2017 dialog stooped responding and then crashes out.
It produces no errors and I checked the event log couldnt see anything linking to scafollding
Also are their any scaffolding templates that output Bootstrap comp table forms ?.
public class basketheader
{
[Key]
public int id { get; set; }
public int orderId { get; set; }
public string title { get; set; }
public string description { get; set; }
public string emailBasket { get; set; }
public decimal basketTotal { get; set; }
public int currency { get; set; }
public bool isDeleted { get; set; }
public bool isActive { get; set; }
public DateTime createDated { get; set; }
public bool isSaved { get; set; }
public string notes { get; set; }
public int basketSessionId { get; set; }
public string EnteredBy { get; set; }
public BasketStatus BasketStatusSetting { get; set; }
public enum BasketStatus
{
Basket=0,
Order = 1,
Abandon=2,
FailedPayment=3,
Discarded=4
}
}
Basket Lines
public class basketlines
{
[Key]
public int id { get; set; }
public int basketId { get; set; }
public int productId { get; set; }
public string productName { get; set; }
public int qty { get; set; }
public decimal price { get; set; }
public decimal lineTotal {get;set;}
public decimal Total { get { return qty * price; } }
public bool isVisible { get; set; }
public bool isDeleted { get; set; }
public string EnteredBy { get; set; }
}
I managed to find an error in my event viewer but not sure if it related to scafoolding directly
Faulting application name: devenv.exe, version: 15.0.26430.16, time stamp: 0x5968f055
Faulting module name: PresentationCore.ni.dll, version: 4.7.2102.0, time stamp: 0x5937170a
Exception code: 0xc0000005
Fault offset: 0x00245bdb
Faulting process id: 0xaf0
Faulting application start time: 0x01d33538be99ac24
Faulting application path: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.exe
Faulting module path: C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\PresentationCore\e455d647c69dde661983d913460fef16\PresentationCore.ni.dll
Report Id: a9a59056-e157-4f3c-a1b2-23a695355c2b
Faulting package full name:
Faulting package-relative application ID:

Unable to get the correct regex pattern to change a property into a value setter

Assuming I have some object like, with LOTS of properties:
public class SomeObject
{
public SomeOtherObject1 Property1 { get; set; }
public SomeOtherObject2 Property2 { get; set; }
public SomeOtherObject3 Property3 { get; set; }
public SomeOtherObject4 Property4 { get; set; }
public SomeOtherObject5 Property5 { get; set; }
public SomeOtherObject6 Property6 { get; set; }
}
It would be really cool if I could create a constructor and copy the properties into the constructor...
public class SomeObject
{
public SomeObject
{
public SomeOtherObject1 Property1 { get; set; }
public SomeOtherObject2 Property2 { get; set; }
public SomeOtherObject3 Property3 { get; set; }
public SomeOtherObject4 Property4 { get; set; }
public SomeOtherObject5 Property5 { get; set; }
public SomeOtherObject6 Property6 { get; set; }
}
public SomeOtherObject1 Property1 { get; set; }
public SomeOtherObject2 Property2 { get; set; }
public SomeOtherObject3 Property3 { get; set; }
public SomeOtherObject4 Property4 { get; set; }
public SomeOtherObject5 Property5 { get; set; }
public SomeOtherObject6 Property6 { get; set; }
}
And use Visual Studio's Find And Replace with Regex to change the highlighted lines in the constructor from:
public SomeOtherObject1 Property1 { get; set; }
public SomeOtherObject2 Property2 { get; set; }
public SomeOtherObject3 Property3 { get; set; }
public SomeOtherObject4 Property4 { get; set; }
public SomeOtherObject5 Property5 { get; set; }
public SomeOtherObject6 Property6 { get; set; }
to:
this.Property1 = new SomeOtherObject1();
this.Property2 = new SomeOtherObject2();
this.Property3 = new SomeOtherObject3();
this.Property4 = new SomeOtherObject4();
this.Property5 = new SomeOtherObject5();
this.Property6 = new SomeOtherObject6();
First I tried:
public\s{:i}\s{:i}\s{\sget;\sset;\s}
this.\2 = new \1();
Then I thought maybe it was a line issue, so I tried:
^\s*public\s{:i}\s{:i}\s{\sget;\sset;\s}.$
this.\2 = new \1();
Anyone else have any thought on how to get this to work?
You need to escape the {} around get; set;. Also, I've used :b instead of \s and allowed for more than one. Here:
public:b+{:i}:b+{:i}:b*\{:b*get;:b*set;:b*\}
And as you wrote:
this.\2 = new \1();

Microsoft Moles : circular reference error?

I've the following Entity Framework entities:
public class Country
{
public long Id { get; set; }
public string Code { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Person
{
public long Id { get; set; }
public long? Country_Id { get; set; }
public Country HomeCountry { get; set; }
}
Moles has generated MPerson and MCountry stub classes.
Now I do want to stub the set of the Country_Id:
MPerson.AllInstances.Country_IdSetNullableOfInt64 = (Person instance, long? id) =>
{
// Do something
// Set the Country_Id to the provided id
// This will trigger this same method again and again. How to avoid this ?
instance.Country_Id = id;
};
This post gives the answer:
MolesContext.ExecuteWithoutMoles(() => instance.Country_Id = id);