We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to generate columns from data annotations

Thread ID:

Created:

Updated:

Platform:

Replies:

143816 Apr 5,2019 05:03 PM UTC Apr 9,2019 12:56 PM UTC ASP.NET Core - EJ 2 3
loading
Tags: DataGrid
derekb
Asked On April 5, 2019 05:03 PM UTC

Instead of manually specifying grid columns, like this:
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100" validationRules="@(new{ required: true })"></e-grid-column>                
                <e-grid-column field="CustomerID" headerText="Customer ID"   type="string" width="120" validationRules="@(new { required: true, minLength: [customFn, 'Need atleast 5 letters'] })></e-grid-column>                
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
            </e-grid-columns>
</ejs-grid>

I would like to be able to generate these columns automatically from data annotations.

I've seen other forum posts about using the @Html.EJ() and/or @Html.EJS() extensions to create the grid, but I have no idea
where these extensions live. I've tried installing the Syncfusion.EJ2.MVC5 package, but it breaks my project (tag helpers are no longer recognized).

Thavasianand Sankaranarayanan [Syncfusion]
Replied On April 8, 2019 10:28 AM UTC

Hi Derekb,

Greetings from Syncfusion.

Query: How to generate columns from data annotations,


Instead of manually specifying grid columns, like this:


We have validated your query and we suspect that you want to generate columns from server side instead of mentioning columns in view page. We have created a

sample based on your requirement. Please find the below code example and sample for your reference.


[code example]

...
namespace
TestSample.Controllers
{

public
class
HomeController
: Controller
{

public
IActionResult Index()
{
ViewBag.DataSource = OrdersDetails.GetAllRecords().ToList();

ViewData[
"columns"] =
new
List()

{
new
Syncfusion.EJ2.Grids.GridColumn() { Field =
"OrderID", HeaderText =
"Order ID", Width =
"120", IsPrimaryKey =
true },

new
Syncfusion.EJ2.Grids.GridColumn() { Field =
"CustomerID", HeaderText =
"Customer ID", Width =
"120", EditType="dropdownedit" },

new
Syncfusion.EJ2.Grids.GridColumn() { Field =
"ShipCity", HeaderText =
"Ship City", Width =
"120"},

new
Syncfusion.EJ2.Grids.GridColumn() { Field =
"ShipCountry", HeaderText =
"Ship Country", Width =
"120" }
};

return
View();
}
}

public
class
OrdersDetails
{

public
static
List order =
new
List();

...
}
<div>

<ejs-grid
id="Grid"
dataSource="ViewBag.DataSource"
allowResizing="true"
allowPaging="true"
columns="@ViewData["columns"]"
allowSorting="true"
toolbar="@(new
List<
string>() {"Add",
"Edit",
"Delete",
"Cancel",
"Update"})">

<e-grid-editSettings
allowAdding="true"
allowDeleting="true"
allowEditing="true"
mode="Dialog">e-grid-editSettings>

ejs-grid>
div>


Sample link:


http://www.syncfusion.com/downloads/support/directtrac/general/ze/TestSampleCoreSColumns-987531725


Please get back to us if you need further assistance.


Regards,


Thavasianand S.

derekb
Replied On April 8, 2019 05:54 PM UTC

Hi Thavasianand,

Thank you for the reply. Perhaps I was unclear with what I need, so I will rephrase.

What I would like to do is generate a grid without specifying the grid columns at all. With the code you posted, you still need to specify:
  1. Field attribute
  2. HeaderText attribute
  3. ValidationRules attribute
  4. Type attribute
I don't want to populate these attributes manually. Field should be inferred from the name of the model property, HeaderText should be inferred from any [Display(Name = "header name")] annotations, ValidationRules should be inferred from annotations such as [RegularExpression] / [Required] etc., and the type should either be inferred from the type of the property, or from any [DataType(DataType.DateTime)] annotations (for example).

So, I would like to be able to do something like: @(Html.EJS().Grid<Model>()...) without needing to specify columns manually (unless I need to override something).
Is there a way I can accomplish this? Also, would you mind linking to any relevant documentation if available?

Thanks,
DerekB


Thavasianand Sankaranarayanan [Syncfusion]
Replied On April 9, 2019 12:56 PM UTC

Hi Derekb, 
 
We have validated your query and we have already considered “Data Annotation support for Grid in ASP.Net Core” as a feature request list. The feature will be available in our Essential Studio 2019, Volume 1 SP1 release. We will update your once the release is rolled out. Until then we appreciate your patience. You can keep track of the feature from the feedback portal below. 
 

Regards, 
Thavasianand S. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon