Anjali
Anjali

Reputation: 2698

Populating drop down with Entity Framework

I have the following table bookAssignment:

ID   Group   OFFICE  name  FKOffice     FKGroup
---------------------------
1    Test1      X   West     1          1
2    Test3      Y   North    2          2
3    Test1      Z   South    3          1
4    Test1      P   South    4          1

I have around 3000 rows in this table where ID is unique and primary key. GROUP and Office has lot of repeated value.

I have another table for Office and Group

tblOffice

OfficeID    OfficeName
1            X
2            Y
3            Z
4            P

tblGroup

GroupID     GroupName
1            Test1
2            Test3
3            Test6

There is a relationship between BookAssignment table and tblGroup and tblOffice. FKOffice and FKGroup are the foreign keys in BookAssignment table.

I have the following model class:

 public partial class bookAssignment_new
{
    public int ID { get; set; }
    public Nullable<int> Book { get; set; }
    public string TechName { get; set; }
    public string Office { get; set; }
    public IEnumerable<SelectListItem> OfficeList { get; set; }
    public IEnumerable<SelectListItem> GroupList { get; set; }
    public virtual GroupName GroupName { get; set; }
    public virtual OfficeName OfficeName { get; set; }
}

My GroupName Model is like so:

public partial class GroupName
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public GroupName()
    {
        this.bookAssignment_new = new HashSet<bookAssignment_new>();
    }

    public int GroupID { get; set; }
    public string Groups { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<bookAssignment_new> bookAssignment_new { get; set; }
}

My OfficeName Model is like so:

public partial class OfficeName
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public OfficeName()
        {
            this.bookAssignment_new = new HashSet<bookAssignment_new>();
        }
    
        public int OfficeID { get; set; }
        public string Office { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<bookAssignment_new> bookAssignment_new { get; set; }
    }

I want to populate two drop downs on my view with the Office and group.

Below is my controller code:

  public ActionResult Index()
    {
        
        return View();
    }

How can I make a Group and Office drop down in my view and populate it with Group and Office drop down values. Below is what my structure looks like:

enter image description here

Thank You.

Upvotes: 0

Views: 57

Answers (2)

Abhishek Borikar
Abhishek Borikar

Reputation: 434

Here your are making two big mistakes. First you are just creating instance instance on your model class i.e. bookAssignment_new. You need to populate that.

Second one is you are applying LINQ on string .WHERE() .Change Group and Office to list to apply any LINQ.

Upvotes: 1

Felix
Felix

Reputation: 10078

First, don't put OfficeList and GroupList into the model. Assign it to ViewData instead.

Second, bookassignment_new is a class representing a single object. You probably have some way of populating a List of those. You can get values from that list by running LINQ query with Distinct() function.

So - your model in the controller is incorrect. That's why you are getting an error. Fix that first - and fixing dropdowns will be much easier

Upvotes: 0

Related Questions