locked
How to apply a filter to a foreign key lookup in the Details View? RRS feed

  • Question

  • User1929428035 posted

    I have my DD EF project with a filtered gridview in the List.aspx file. The gridview lists student attendance records and is filtered by the class the students are in which I do in the DomainService.cs file. When I attempt to insert a new attendance record the details view (i.e. Details.aspx) has a drop down for the foreign key StudentID which lists all the students. So I want to filter the drop down list in much the same way the gridview is filtered.

    Monday, June 17, 2013 1:26 PM

Answers

  • User1929428035 posted

    I solved it. Here is what I did. I found out that the ForeignKey_Edit.ascx is the user control that has the foreign key dropdownlist. In the code behind there is a PopulateListControl() method. So I wrote my own PopulateListControl method and placed a condition around the PopulateListControl()  method as follows:

    if (HttpContext.Current.User.IsInRole("Teacher"))
    {
         populateListControlStudentsFromaClass(DropDownList1);
    }
    else
    {
         PopulateListControl(DropDownList1);
    }


    Worked like a charm.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 20, 2013 3:58 AM

All replies

  • User-1716253493 posted
    http://msdn.microsoft.com/en-us/library/aa581796.aspx
    Monday, June 17, 2013 1:35 PM
  • User1929428035 posted

    I'm sorry I did not explain myself properly. I have my DD project with a filtered gridview in the List.aspx file. The gridview lists student attendance records and is filtered by the class the students are in. When I attempt to insert a new attendance record the details view (i.e. Details.aspx) has a drop down for the foreign key StudentID which lists all the students. So I want to filter the drop down list in much the same way the gridview is filtered. Hope this helps.

    Monday, June 17, 2013 2:39 PM
  • User-330204900 posted

    Hi there, what you need to do is add the code for this filtering to the Get method of the entity in the domain service. You could create a custom field template but for me that defeats the object of the DS

    Monday, June 17, 2013 3:08 PM
  • User1929428035 posted

    I am not sure I follow. By the get method are you refering to: 

    public IQueryable<StudentAttendance> GetStudentAttendances()

    If so then I think I did that:

    		[RequiresAuthentication]
    		[RequiresRole("Clerk", "Teacher", "Principal", "Parent")]
    		public IQueryable<StudentAttendance> GetStudentAttendances()
    		{
    			SIMSEntities db = new SIMSEntities();
    
    			if (this.ServiceContext.User.IsInRole("Parent"))
    			{
    				List<int> intListofStudentIDs = new List<int>();
    				intListofStudentIDs = getStudentIDsByParent(getTeacherID());
    
    				return this.ObjectContext.StudentAttendances.Include("Student").Where(s => intListofStudentIDs.Any(i => i == s.StudentID)).OrderBy(s => s.StudentID);
    			}
    			
    
    			if (this.ServiceContext.User.IsInRole("Teacher"))
    			{
    				List<int> listofstudents = new List<int>();
    				listofstudents = getStudentsByClassID();
    				return this.ObjectContext.StudentAttendances.Include("Student").Where(s => listofstudents.Any(i => i == s.StudentID)).OrderBy(s => s.StudentID);
    			}
    			return this.ObjectContext.StudentAttendances.Include("Student").OrderBy(s => s.Student);
    		}



    Monday, June 17, 2013 3:14 PM
  • User-330204900 posted

    The reason you woudl do it this way is all operation that are accessing that entity collection woudl then get the list pre-filtered. if this is not working add a break point in the Get method of the entity and see if the code is being executed correctly :)

    Monday, June 17, 2013 3:54 PM
  • User1929428035 posted

    The filter is being executed for the gridview but not for the details view.

    Monday, June 17, 2013 4:17 PM
  • User-330204900 posted

    it should always be executed, that how a DS works it uses convention to call the Get method for all select queries.

    Monday, June 17, 2013 4:24 PM
  • User-1716253493 posted

    Simply Pass ID and Class QueryString using HyperlinkField in GridView

    <asp:HyperLinkField DataNavigateUrlFields="StudentID,Class" 
    DataNavigateUrlFormatString="Detail.aspx?StudentID={0}&class={1}" 
    Text="Detail" />

     

    Monday, June 17, 2013 9:55 PM
  • User1929428035 posted

    The StudentAttendance Table has the following fields:

    ID, StudentID, Date, Present (boolean), and Late (boolean).

    The ClassID is referenced from a Join table ClassStudents:

    ID, ClassID, StudentID.

    So in the DomainService.cs file I select a list of StudentIDs from a selected Class (ClassID).

    As such I'm not sure your suggestion will work but I will try it. Thank you for you advice.

    Monday, June 17, 2013 10:13 PM
  • User1929428035 posted

    oned_gk I have tried and could not implement your suggestion. Thank you for your contribution.

    Tuesday, June 18, 2013 4:44 AM
  • User-330204900 posted

    I must reiterate that if you are using Domain Service the Get shoudl always be called for any select query, your project must be at fault becase this is not happening I will make a sample and test and get back to you.

    Tuesday, June 18, 2013 4:59 AM
  • User1929428035 posted

    Thank you Steve.

    Tuesday, June 18, 2013 5:03 AM
  • User-330204900 posted

    Hi there, I have just build a very simple Domain Service project sample and it works as I described. I believe your issue is that there is either somthing wrong with your Dynamic Data Domain Serive Project or there is some configuration wrong.

    Example Project is here DD_DS_Northwind.zip this is on my skydrive and so the link will simply take you to my public folder just look for the named zip file unzip and then configure the connection string, it's set for Northwind on (local) and run Open the Products edit insert you will see categories is filtered to just items that begin with the letter 'C'.

    Tuesday, June 18, 2013 5:30 AM
  • User1929428035 posted

    I solved it. Here is what I did. I found out that the ForeignKey_Edit.ascx is the user control that has the foreign key dropdownlist. In the code behind there is a PopulateListControl() method. So I wrote my own PopulateListControl method and placed a condition around the PopulateListControl()  method as follows:

    if (HttpContext.Current.User.IsInRole("Teacher"))
    {
         populateListControlStudentsFromaClass(DropDownList1);
    }
    else
    {
         PopulateListControl(DropDownList1);
    }


    Worked like a charm.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 20, 2013 3:58 AM
  • User-330204900 posted

    Hi there, this is a work around that I use when not using Domain Service.

    Thursday, June 20, 2013 6:24 AM