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.
Hello,
Thank you for your post.
I have some questions concerning this matter. As I understand you have one Grid, and by saying "It is dynamic" you mean that the Grid definition (features, options, etc) is always the same, although the DataSource and PrimaryKey can be different? Also you have action method that give you the model from which the Grid is initialized and with switch-case statement, you give different DataSource and PK. Please let me know if I am missing something from your scenario? So you question is if it is possible the Grid to automatically detect the Key annotation and to use it like PK?
Code snippet for the Key annotation:
Looking forward to hearing from you.
Yes that's exactly what i am aiming for. I mean , if I have set the [Key] attribute on my models & I have set autogeneratecolumns(true) than can the grid detect the primary key automatically by extracting the [Key] annotation on my usr_Username property? Because if it can select automatically than i would'nt have to explicitly store the primary keys of all my table in a list and set the appropriate key every time a grid model is initialized.
And is my approach right in setting the datasource using switch case statements ? Is there a better way to do this? All the features like sorting,filtering,selection,paging are going to be same for every model except the datasource & PK.
And if in future I want to hide a particular column , is it possible that I specify [HiddenInput(DisplayValue = false)] on usr_Password column in my models class and the grid automatically hides the hidden attribute column.
[
HiddenInput
(DisplayValue = false
)
]
Also the column names are not automatically detected by the grid because i have specified [DisplayName("Username")], but it shows usr_Username.
Please note that I have set autogeneratecolumns(true) therefore i cannot specify the column layouts explicitly because my grid is generic and it will be supplied with different models at runtime.
My User model is as follows.
-------------------------------------------------------------
namespace COMMON.Models{ public class User (my User model, its datasource is set at runtime) { [Key] [DisplayName("Username")] public string usr_Username { get; set; }
[HiddenInput(DisplayValue=false)] [DisplayName("Password")] public string usr_Password { get; set; } [DisplayName("Fullname")] public string usr_Fullname { get; set; }
}
public class Role (another model ,its datasource will also be set at runtime depending on the selected model/entity)
{ ....}
Please also note that my models classes are in a separate class library under the folder COMMON>Model , This is not the same models folder as in an MVC Project, This Models folder in a class library, which is shared among all the projects included in my solution , including DAL,BAL,MVCWebApp. I have added the following reference in my Models to use the data annotations provided by MVC:
using System.ComponentModel.DataAnnotations;
Any help would be appreciated. Looking forward to your reply. Thanks
If you have any other questions feel free to ask.