Click here to Skip to main content
15,867,306 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi ,
I am facing problem to use validation for child property .Here is my scenario
I have a POCO Class like this which is self generated:

C#
namespace JQGridTest.Models
{
    using System;
    using System.Collections.Generic;

    
    public partial class SR_WorkDescription
    {
        public SR_WorkDescription()
        {
            this.SR_ResourceWithWork = new HashSet<SR_ResourceWithWork>();
        }
    
        public int WId { get; set; }
        public Nullable<int> ProjectId { get; set; }
        public Nullable<long> StatusId { get; set; }
       
        public string Description { get; set; }
        public string StartDate { get; set; }
        public string CompletionDate { get; set; }
    
        public virtual SR_Project SR_Project { get; set; }
        public virtual ICollection<SR_ResourceWithWork> SR_ResourceWithWork { get; set; }
        public virtual SR_Status SR_Status { get; set; }
    }
}



I also add manually a model in which rhe property of the above class is defined

C#
namespace JQGridTest.Models
{
    public class WorkModel
    {
        [Required]
        public string SelectedStatus { get; set; }
        public List<SR_Status> StatusList { get; set; }

        [Required]
        public string SelectedProject { get; set; }
        public List<SR_Project> ProjectList { get; set; }
        [Required]
        public string SelectedResource { get; set; }
        public List<SR_Resource> ResourceList { get; set; }
        [Required]
        public SR_WorkDescription WorkDescription { get; set; }//I want to apply validation on this property's child property
  }
}




I also define a metadata type to apply validation
C#
[MetadataType(typeof(SR_WorkDescriptionMetaData))]
   public partial class SR_WorkDescription
   {
       private class SR_WorkDescriptionMetaData
       {
           [Required]
           public object Description;



       }
   }


In my view I am doing like this
C#
<td>@Html.TextAreaFor(x => x.WorkDescription.Description)
                        @Html.ValidationMessageFor(x => x.WorkDescription.Description, "Please Enter Description")
                    </td>


Through this i want to apply validation on "Description" field which lies in SR_WorkDescription
Please help me to sort out the problem
Thanks !!
Posted
Updated 13-Nov-13 0:36am
v4
Comments
Er Daljeet Singh 14-Nov-13 5:31am    
can you please explain the hierarchy of the classes.
please name all the classes you have created or created by .EDMX file.
one more tell from which class you are inheriting your view.
Antariksh Verma 14-Nov-13 5:34am    
SR_WorkDescription class created with the help of .edmx file
class WorkModel is created in model manually
[MetadataType(typeof(SR_WorkDescriptionMetaData))]
public partial class SR_WorkDescription
The class added via edmx file is partial do redife it again as Metadatatype so that required field can be set.

I am inheriting a class Sr_WorkDescription which is generated via .edmx
Er Daljeet Singh 14-Nov-13 7:22am    
your code in little bit confusing.You are inheriting you view from Sw_workdescription class but there is now property named WorkDescriptionther is no link between Sw_worddescription class and your WorkModel class that is confusing.Try to put your whole code i.e. your view and model files
Er Daljeet Singh 14-Nov-13 7:24am    
Go through this link
http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/
Antariksh Verma 14-Nov-13 7:39am    
Thanks for suggestion

public SR_WorkDescription WorkDescription { get; set; }

this property make a relation between those files which is in WorkModel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900