Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
715
Detecting the primary key automatically
posted

Hi,

I am using Asp.Net MVC to render my grid. And my grid model is written in MVC controllers action method. The method GetGridModel() returns the required GridModel , inside the GetGridModel() method i set features and properties of the grid with respect to the requested datasource.

My grid is designed to be dynamic hence the code in GetGridModel is almost generic except DataSource & PrimaryKey. To set both these properties i am using switch case statements to detect which model is requested. I set the primary key like this:

 gridModel.PrimaryKey = selectedentity.PrimaryKey;      (where selected entity contains the selected item in my list which is an instance of ListingModel class)

& heres my listing model ->  list.Add(new ListingModel("Users", "GetUsers", "usr_Username"));        (Here i have to store the primary keys of all the entities)
                                        

Now DataSource is i think required and I have to use switch case statements in order to set the correct datasource of the grid but i was thinking of getting the primary key automatically .Because my models have [Key] annotation set on the primary key property.

Isnt it possible that the grid detects the primary key automatically because i have specified it in my model class.

If this is possible then this would save a lot of code replication and I would not be bound to store the primary keys of all the models in my ListingModel.

Please tell me an alternative.

thanks in advance.