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.
If you have any other questions feel free to ask.
Hello,
Thank you for the detailed explanation.
About your questions. Yes, your approach is good, to use switch case statement for changing the Datasource. Regarding the Data Annotations specified in the Model, currently our Grid do not handle them and if you have set [Key], [DisplayName] or [HiddenField] annotation this will not be reflect on the Grid. About this feature you can suggest this as a new product idea for future versions at <http://ideas.infragistics.com>.
Steps to create your idea:
1. Log into the Infragistics Product Ideas site at http://ideas.infragistics.com (creating a new login if needed).2. Navigate to the product / platform channel of your choice (e.g. WPF, Windows Forms, ASP.NET, HTML5 / Ignite UI, iOS / NucliOS, etc.)3. Add your product idea and be sure to be specific and provide as much detail as possible. Explain the context in which a feature would be used, why it is needed, why it can’t be accomplished today, and who would benefit from it. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. Be convincing!
The benefits of submitting the product idea yourself include:
- Direct communication with our product management team regarding your product idea.- Notifications whenever new information regarding your idea becomes available.
Additional benefits of the product idea system include:- Ability to vote on your favorite product ideas to let us know which ones are the most important to you. You will have ten votes for this and can change which ideas you are voting for at any time.- Allow you to shape the future of our products by requesting new controls and products altogether.- You and other developers can discuss existing product ideas with members of our Product Management team.
The product ideas site allows you to track the progress of your ideas at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for 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
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.