This is a migrated thread and some comments may be shown as answers.

EnableModelValidation not working??

4 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Luki
Top achievements
Rank 1
Luki asked on 26 Jun 2012, 10:35 AM

Hi, I've a grid:

<telerik:RadGrid ID="Grid" runat="server" GridLines="None" AllowPaging="True" CssClass="RadGrid"
    AllowSorting="True" AutoGenerateColumns="false" ShowStatusBar="true" AllowFilteringByColumn="true">
    <ValidationSettings EnableValidation="true" EnableModelValidation="true" CommandsToValidate="Insert,Edit,Update" />
    <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="Id" InsertItemPageIndexAction="ShowItemOnCurrentPage">

And I am using EF as Datasource (with RepositoryPattern).

Furthermore I user Dataannotations - so every Entity has a partial MetadataClass with Annotations like:

[MetadataType(typeof(entityMetadata))]
    public partial class entity
    {
        internal sealed class entityMetadata
        {
          
            [Required(ErrorMessage="Id is required")]
            public Int32 Id { get; set; }
  
            [Required(ErrorMessage="Name is required")]
            [StringLength(400)]
            public String Name { get; set; }
  
            [Required(ErrorMessage="Description is required")]
            public String Description { get; set; }
  
        }
    }

I thought with "EnableModelValidation" the Grid would automatically validate against this annotations.
  But it doesn't. 

My UpdateCommand looks like this:

void Grid_UpdateCommand(object sender, GridCommandEventArgs e)
{

   using (IGenericRepository<Lov_KpiType> repositroy = new GenericRepository<Lov_KpiType>())
   {
   GridEditableItem item = e.Item as GridEditableItem;
   int kpiType_Id = Convert.ToInt32(item.GetDataKeyValue("Id").ToString());
   Lov_KpiType kpiType = repositroy.FindById(kpiType_Id);

   item.UpdateValues(kpiType);
   repositroy.SaveChanges();
   }
}

   

The item is not updated and the Grid changes to TemplateMode again. No errormessage is show at this time..

I thought, as it is in MVC, when Data-Annotations are used - no Postback is done when ClientSideValidation fails.

Furthermore I do not know, how to determine if there is an validation-error in code behind.

Please help :)

4 Answers, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 29 Jun 2012, 06:42 AM
Hi Lukas,

The RadGrid for ASP.NET AJAX does not support DataAnnotations. You need to know what control will be validated in order to add an appropriate validator.  In this case you could add dynamically the validation controls to an edit field in Telerik RadGrid . Please check out the following article which explains how to add a validator of your choice to the edit field:
http://www.telerik.com/help/aspnet-ajax/grid-validation.html

Please give it try and let me know if you experience any problems. Looking forward for your reply.

All the best,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Luki
Top achievements
Rank 1
answered on 29 Jun 2012, 07:04 AM

Thank you,

I've now implemented it the old-fashion way with asp.net validatiors.

However, there are some ways to use data-annotations in webforms:

http://www.codeproject.com/Articles/95158/Building-ASP-NET-Validator-using-Data-Annotations

greets,

lukas

0
Mark
Top achievements
Rank 1
answered on 10 Sep 2012, 03:28 PM
Hi Radoslav,

So if setting EnableModelValidation="true" doesn't validate the model (using DataAnnotations), what exactly does this property do?

Thanks,
Mark


0
Radoslav
Telerik team
answered on 13 Sep 2012, 06:52 AM
Hello Mark,

Unfortunately our controls do not support DataAnnotations. The EnableModelValidation property gets or sets a value that indicates whether a validator control will handle exceptions that occur during insert or update operations.

I hope this helps.

Kind regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Luki
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Luki
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Share this question
or