Dynamic data: how to filter a drop-down list for a foreign key on an edit page

Using Linq-to-SQL and dynamic data.

On the dynamic data editing screen, the possible values โ€‹โ€‹for the foreign key are listed in the drop-down list.

I need to filter out the values โ€‹โ€‹listed in this dropdown, preferably by adding a where clause in the linq-to-sql query.

Any ideas?

+4
source share
3 answers

Well, I found a place to configure this, although I still haven't found a way to override or extend the linq request to load data:

In the \ DynamicData \ FieldTemplates \ ForeignKey_Edit.ascx.cs file (in accordance with the default position), the following call is made in the Page_Load method:

PopulateListControl(DropDownList1); 

This can be completely replaced by your own code. In my case, I first checked if the current table implements the user interface that I defined. If so, use this to get the data to bind the drop down list to, otherwise pass control to the PopulationListControl.

Using a Reflector, it seems that PopulateListControl ultimately uses MetaTable.GetQuery () to get the default query for the table. It would be great to expand this, but instead, let's move on to other things!

+5
source

I think you have already solved your problem, but I would like to register my solution, just in case, this could help someone!

I followed the path you started by editing ForeignKey_Edit.ascx.cs. After the line PopulateListControl (DropDownList1), I call the RemoveResultsFromOtherUsers () method.

Here is a way:

  private void RemoveResultsFromOtherUsers() { using (var db = new FinWeb3.DynamicData.FinWebDBDataContext()) { var data = db.GetTable(this.Column.Provider.ColumnType); var removableItems = new List<ListItem>(); foreach (var item in data) { var dbItem = item as IDbEntity; if (dbItem.UserName != this.Context.User.Identity.Name) { removableItems.Add( DropDownList1.Items.FindByValue(dbItem.Id.ToString())); } } foreach (var item in removableItems) { DropDownList1.Items.Remove(item); } } } 

IDbEntity is an interface that implements all my linq classes for sql. This interface has the properties Id and UserName. Here, I intend to delete results that are not registered by the current authenticated user.

Thanks for your question and solution!

+4
source

I would create a custom field template that is a web-based user control and references it in metadata using UIHintAttribute. Note: you will need to create a standard version (usually a hyperlink) and a _Edit version (usually a drop-down list).

0
source

Source: https://habr.com/ru/post/1303463/


All Articles