I'm new to asp.net and thanks to time restrictions I've had to jump in at the deep end, any help you can provide is great.
I'm developing an Asset Register for one of our clients, a rough over view of the database is as follows.
Assets
<div mce_keep="true">asset_id</div>
<div mce_keep="true">asset_tag</div>
<div mce_keep="true">model_id</div>
<div mce_keep="true">asset_serial</div>
<div mce_keep="true">...</div>
Models
<div mce_keep="true">model_id</div>
<div mce_keep="true">manufacturer_id</div>
<div mce_keep="true">model_name</div>
Manufacturers
<div mce_keep="true">manufacturer_id</div>
<div mce_keep="true">manufacturer_name</div>
Using DynamicData I've built a custom page to display the Assets table and I'd like the gridview to display the manufacturer_name field. I've tried using "Model.Manufacturer" and "Model.Manufacturer.manufacturer_name" for the datafield property with no luck,
it doesn't recognise the collumn exists. Is this something that DynamicData has support for, if not can anyone point me in the right direction.
Many Thanks
Ben
linqdatasourceASP.NET Dynamic DataDynamicFieldDynamic Data 5-12 Release
you need to point at the oject name i.e. Manufacturers not manufacturer_id this will cause DynamicData to use the foreign key table if the field name you wish to show in the DropdownList then you will need to attribute it
like so:
[MetadatatypeAttribute(ManufacturersMetaData)] public class Manufacturers
{
}
public class ManufacturersMetaData
{
[DisplayColumnAttribute("manufacturer_name")] public object manufacturer_name {get;
set;}
}
See Dynamic Data Attributes on Maíra Wenzel's Blog you will find a list of most
of the DynamicData Attributes the on I think you need is DisplayColumnAttribute which
"Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds."
Hope this helps
Dynamic DataMetadata
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Steve: thanks, I've just gotten back from a 2 week vacation and not everything has been updated. should be fixed now.
Back to Ben's issue: What I think he is trying to do - and correct me if I'm wrong - is to display an additional column in the Asset's grid view that would display the Asset's Model's Manufacturer's name. This can only be done by having an extra property
on the Asset class to represent the extra column. However, I did make a small mistake in my initial response, and it should be:
public partial class Asset {
public string ManufacturerName {
get {
return this.Model.Manufacturer.manufacturer_name;
}
}
}
I've ha a re-read of his original question and yes I think your answer is what he is looking for. His problem sounded like one I'd had and just jumped to the wrong conclusion.
Oh and your signature correct now [:D]
Dynamic Data
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Thanks for the quick replies, I will give this a try. On a connected issue, I would like to be able to filter a models DropDownList whilst in edit mode using the manufacturers field. I think the best way to do this would be to build a user control with
linq-to-sql datasources and the necessary methods to filter a models DropDownList, what I'm not sure on is how to then get the value of the models dropDownList (i.e. model_id) back to the GridView ready for the update. Do I need to get the user control to
set a property in the assets class?
And lastly on a side note, i remember reading that there were plans to provide an OrderBy attribute to the Dynamic Filters, does anyone know if this has been or can be implented in the new code drop?
You can apply a DisplayColumnAttribute for foreign key tables. This attribute takes SortColumn and SortDecending properties to determine how things are sorted. I know these will be used when editing a row, not sure if these affect the filters as well. Marcin?
Scott Hunter
PM, ASP.NET Team, Microsoft
Marked as answer by TheDuke on May 15, 2008 01:23 PM
Regarding the cascading dropdown issue it should be doable. I had done this before for filters (something like a car shopping site: first choose make, then model, which filters the list of available cars) but the sample was rough and I do not have it available
right now. However, it is a bit tricky as I recall I had to write my own linq queries, and writing something that would be generic enough for all situations is nontrivial. I'll try to get it working again and I will let you know.
TheDuke
Member
7 Points
11 Posts
Accessing a foreign key's table.
May 14, 2008 03:53 PM|LINK
Hi
I'm new to asp.net and thanks to time restrictions I've had to jump in at the deep end, any help you can provide is great.
I'm developing an Asset Register for one of our clients, a rough over view of the database is as follows.
Assets
Models
Manufacturers
Using DynamicData I've built a custom page to display the Assets table and I'd like the gridview to display the manufacturer_name field. I've tried using "Model.Manufacturer" and "Model.Manufacturer.manufacturer_name" for the datafield property with no luck, it doesn't recognise the collumn exists. Is this something that DynamicData has support for, if not can anyone point me in the right direction.
Many Thanks
Ben
linqdatasource ASP.NET Dynamic Data DynamicField Dynamic Data 5-12 Release
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Accessing a foreign key's table.
May 14, 2008 05:32 PM|LINK
Hi Ben,
you could do this by adding a custom property to your Asset class:
public partial class Asset {
public string ManufacturerName { get { return this.Manufacturer.manufacturer_name; } }
}
ASP.NET Team
@marcind
Blog
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Accessing a foreign key's table.
May 14, 2008 05:39 PM|LINK
you need to point at the oject name i.e. Manufacturers not manufacturer_id this will cause DynamicData to use the foreign key table if the field name you wish to show in the DropdownList then you will need to attribute it like so:
[MetadatatypeAttribute(ManufacturersMetaData)]
public class Manufacturers
{
}
public class ManufacturersMetaData
{
[DisplayColumnAttribute("manufacturer_name")]
public object manufacturer_name {get; set;}
}
See Dynamic Data Attributes on Maíra Wenzel's Blog you will find a list of most of the DynamicData Attributes the on I think you need is DisplayColumnAttribute which "Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds."
Hope this helps
Dynamic Data Metadata
Always seeking an elegant solution.
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Accessing a foreign key's table.
May 14, 2008 05:43 PM|LINK
Hi Marcin your Signiture still shows Check out the latest 4/23 Dynamic Data Preview update. [:D] you should be on 5/12 [;)]
Dynamic Data 5-12 Release
Always seeking an elegant solution.
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Accessing a foreign key's table.
May 14, 2008 05:56 PM|LINK
Steve: thanks, I've just gotten back from a 2 week vacation and not everything has been updated. should be fixed now.
Back to Ben's issue: What I think he is trying to do - and correct me if I'm wrong - is to display an additional column in the Asset's grid view that would display the Asset's Model's Manufacturer's name. This can only be done by having an extra property on the Asset class to represent the extra column. However, I did make a small mistake in my initial response, and it should be:
public partial class Asset { public string ManufacturerName { get { return this.Model.Manufacturer.manufacturer_name; } } }ASP.NET Team
@marcind
Blog
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Accessing a foreign key's table.
May 14, 2008 06:11 PM|LINK
I've ha a re-read of his original question and yes I think your answer is what he is looking for. His problem sounded like one I'd had and just jumped to the wrong conclusion.
Oh and your signature correct now [:D]
Dynamic Data
Always seeking an elegant solution.
TheDuke
Member
7 Points
11 Posts
Re: Accessing a foreign key's table.
May 14, 2008 06:15 PM|LINK
Thanks for the quick replies, I will give this a try. On a connected issue, I would like to be able to filter a models DropDownList whilst in edit mode using the manufacturers field. I think the best way to do this would be to build a user control with linq-to-sql datasources and the necessary methods to filter a models DropDownList, what I'm not sure on is how to then get the value of the models dropDownList (i.e. model_id) back to the GridView ready for the update. Do I need to get the user control to set a property in the assets class?
And lastly on a side note, i remember reading that there were plans to provide an OrderBy attribute to the Dynamic Filters, does anyone know if this has been or can be implented in the new code drop?
Many thanks.
filters ASP.NET Dynamic Data
scothu
Participant
1436 Points
291 Posts
Microsoft
Moderator
Re: Accessing a foreign key's table.
May 14, 2008 06:23 PM|LINK
You can apply a DisplayColumnAttribute for foreign key tables. This attribute takes SortColumn and SortDecending properties to determine how things are sorted. I know these will be used when editing a row, not sure if these affect the filters as well. Marcin?
PM, ASP.NET Team, Microsoft
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Accessing a foreign key's table.
May 14, 2008 08:44 PM|LINK
Yes, these should work for filters as well.
Regarding the cascading dropdown issue it should be doable. I had done this before for filters (something like a car shopping site: first choose make, then model, which filters the list of available cars) but the sample was rough and I do not have it available right now. However, it is a bit tricky as I recall I had to write my own linq queries, and writing something that would be generic enough for all situations is nontrivial. I'll try to get it working again and I will let you know.
ASP.NET Team
@marcind
Blog
TheDuke
Member
7 Points
11 Posts
Re: Accessing a foreign key's table.
May 14, 2008 10:13 PM|LINK
EDITED: 14:09 15th May
Got it working, thanks very much Marcin, would appreciate help with the cascading drop downs if you get the time.