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.
Unfortunately, activation email could not send to your email. Please try again.

Using Required Annotation in the create/update button of a grid

Thread ID:

Created:

Updated:

Platform:

Replies:

123372 Mar 11,2016 05:50 AM Mar 17,2016 11:10 AM ASP.NET MVC 3
loading
Tags: Grid
Cholet
Asked On March 11, 2016 05:50 AM

Hi,

I have a grid in javacsript, with editSettings editMode : "dialogtemplate" and a dialogEditorTemplateID.
I use create and  udpate buttons of the grid to display a Dialog for inserting or updating datas.
I  use a Required dataAnnotation with an error message in my model to handle the obligation of setting a value in some fields but
 it didn't work so those fields aren't require and i don't know how display the error message from my annotation.

I put the Required annotation like this :

[Required(ErrorMessage = "My error")]
public String name {get;set;}

In my view i imported the following script :
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

Thanks for the help,     

T. Cholet

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On March 14, 2016 05:41 AM

Hi Cholet,

We understood that you would like to perform Validation using Data Annotation Validators.

Supplier class which holds the data annotation properties of the required columns of Grid and its class name can be given as type of object for the Grid as shown below. This will validate after submitting the form based on the data annotation properties. Refer to the following code example

@(Html.EJ().Grid<Supplier>("FlatGrid")

      .Datasource(ds=>ds.URL("/Home/DataSource")

              .InsertURL("/Home/Insert")

              .UpdateURL("/Home/Update")

              .RemoveURL("/Home/Delete")

          .Adaptor(AdaptorType.UrlAdaptor))

         . . . ..

      .Columns(col =>

      {

          col.Field("Id").HeaderText("Unique ID").IsPrimaryKey(true).Width(75).Add();

            . . .

      })

      )


namespace CRUDandValidation.Models

{

    using System.ComponentModel.DataAnnotations;


    public class Supplier

    {

        [Required(ErrorMessage = "ID is must")]

        public int Id { get; set; }

         [RegularExpression(@"^[a-zA-Z0-9 ]{1,10}$")]

        public string PostalCode { get; set; }

         [EmailAddress(ErrorMessage = "Invalid email address")]

        public string EmailAddress { get; set; }

        [Url]

        public string WebAddress { get; set; }

}


We have prepared a sample that can be downloaded from the following location.

Sample: http://www.syncfusion.com/downloads/support/forum/123372/ze/DataAnnotationValidation-1122801197

Regards,
Seeni Sakthi Kumar S.

Cholet
Replied On March 16, 2016 06:40 AM

It works thank you !
Is it possible to do it with javascript ?

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On March 17, 2016 11:10 AM

Hi Cholet

JavaScript Grid does not support data annotations. But you can enable validations using ValidationRules property of Grid Colums. Refer to the following code example.

<div id="Grid"></div>

<script type="text/javascript">

    $(function () {

        $("#Grid").ejGrid({

            dataSource: ej.DataManager({

                url: "/Home/DataSource",

                insertURL: "/Home/Insert",

                updateURL: "/Home/Update",

                removeURL: "/Home/Delete"

            }),

            allowPaging: true,

             . ..  . . .

            columns: [

                    { field: "Id", isPrimaryKey: true, validationRules: { required: true, messages: { required: "ID is must" }} },

                    { field: "CompanyName", validationRules: { required: true, minlength: 3 } },

                    { field: "PostalCode", validationRules: { range: [0, 1000] } }

                     . . . . .


            ]

        });

    })
</script>


Refer to the following Help document.

http://help.syncfusion.com/js/api/ejgrid#members:columns-validationrules

You could also customize the validation rules and frame your own rules. Refer to the following online demo.

http://js.syncfusion.com/demos/web/#!/azure/grid/editing/customvalidation

We have prepared a sample that can be downloaded from the following location.

Sample: http://www.syncfusion.com/downloads/support/forum/123372/ze/CRUDandValidation373767504

Regards,
Seeni Sakthi Kumar 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.

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.

;