Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi.,

I am new to Entity Framework, I have created a EDM using Model first appraoch and applied validations using datannotations, everything is working fine but all the validations are displayed at one place but i would like to show validation error messages beside the respective field.

The code i written is as below

public partial class Hardware_services_repairs
{
public class hardwaremetadata
{
[Required]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage="Numbers and Special characters are not allowed")]
public string CompanyName { get; set; }
[Required]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage="Numbers and Special characters are not allowed")]
public string ContactName { get; set; }
//[Required]
[RegularExpression(@"^[0-9+-]+$",ErrorMessage="Only numbers are allowed")]
public string PhoneNumber { get; set; }
//[Required]
[RegularExpression(@"^[0-9+-]+$", ErrorMessage = "Only numbers are allowed")]
public string MobileNumber { get; set; }
//[Required]
[RegularExpression(@"^[A-Za-z0-9#: ]+$", ErrorMessage = "Special Characters are not allowed")]
public string Address { get; set; }
}
}

The output is as shown below

List of validation errors
Numbers and Special characters are not allowed
Numbers and Special characters are not allowed
Only numbers are allowed
Only numbers are allowed
Special Characters are not allowed


But i want every error message separtely beside the respective field, kindly let me the solution

Thanks in advance


[edit]Code block added[/edit]
Posted
Updated 23-Jun-13 23:59pm
v4
Comments
Jameel VM 24-Jun-13 3:25am    
where you are try to display the validation message?can you elaborate your requirement?
naikotiarun 24-Jun-13 3:38am    
while inserting data i need to display the error messages beside the respective fields in insert.aspx page
Jameel VM 24-Jun-13 4:15am    
which technology you are using?ASP.NET Webform or ASP.NET MVC?
naikotiarun 24-Jun-13 4:27am    
I am using ASP.NET webform

 
Share this answer
 
Can you show me your view code....

It should be like this view code

@using (@Html.BeginForm("ImportContacts", "Outlook", FormMethod.Post, new { id = "Importform" }))
{
@Html.ValidationSummary(true, "Please fill the form correctly and try again.");



@Html.LabelFor(model => model.FirstName)*

@Html.TextBoxFor(mode => Model.FirstName)

@Html.ValidationMessageFor(model => Model.FirstName)




@Html.LabelFor(model => model.LastName)*

@Html.TextBoxFor(mode => Model.LastName)

@Html.ValidationMessageFor(model => Model.LastName)




@Html.LabelFor(model => model.Title)

@Html.TextBoxFor(mode => Model.Title)






@Html.LabelFor(model => model.BusinessTelephoneNumber)*

@Html.TextBoxFor(mode => Model.BusinessTelephoneNumber)

@Html.ValidationMessageFor(model => Model.BusinessTelephoneNumber)



@Html.LabelFor(model => model.MobileTelephoneNumber)

@Html.TextBoxFor(mode => Model.MobileTelephoneNumber)






@Html.LabelFor(model => model.BusinessFaxNumber)

@Html.TextBoxFor(mode => Model.BusinessFaxNumber)

 




@Html.LabelFor(model => model.Email1Address)*

@Html.TextBoxFor(mode => Model.Email1Address)

@Html.ValidationMessageFor(model => Model.Email1Address)






<input type="submit" id="submitform" value="Save" />
<input type="button" id="buttonclose" value="Cancel" onclick="closeWindow()" />


<script type="text/javascript">

/*
jquery called on form submit to save the data.
on Success closes the popup form.
*/
$('#Importform').submit(function (e) {
e.preventDefault();
$.post(this.action, $(this).serialize(), function (response) {
// alert(response.toString());
if (response.toString().toLowerCase() == "success") {
//alert(response.error);
$("#Importform").closest(".k-window-content").data("kendoWindow").close();
}
}, 'json');
});

/*
javascriot method called on click of close button.
closes the popup form.
*/
function closeWindow() {
$("#buttonclose").closest(".k-window-content").data("kendoWindow").close();

}

</script>


}

The model code for this is.....
C#
public class OutlookContact
   {
       [Display(Name="Title")]
       public string Title { get; set; }
       [Required(ErrorMessage="Please enter first name.")]
       [Display(Name="First Name")]
       public string FirstName { get; set; }
       [Required(ErrorMessage = "Please enter last name.")]
       [Display(Name = "Last Name")]
       public string LastName { get; set; }
       [Required(ErrorMessage="Please enter email address.")]
       [Display(Name="Email")]
       [RegularExpression(@"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$", ErrorMessage = "Invalid email address")]
       [DataType(DataType.EmailAddress, ErrorMessage = "Invalid email address")]
       public string Email1Address { get; set; }
       public string BusinessAddressStreet { get; set; }
       public string BusinessAddress { get; set; }
       public string BusinessAddressCity { get; set; }
       public string BusinessAddressState { get; set; }
       [Display(Name="Phone")]
       [Required(ErrorMessage = "Please enter phone number")]
       [RegularExpression(@"^[0-9]{7,15}$", ErrorMessage = "Invalid phone number.")]
       [DataType(DataType.PhoneNumber, ErrorMessage = "Please enter valid phone number")]
       public string BusinessTelephoneNumber { get; set; }
       [Display(Name="Cell")]
       public string MobileTelephoneNumber { get; set; }
       [Display(Name="Fax")]
       public string BusinessFaxNumber { get; set; }
   }
 
Share this answer
 
Comments
naikotiarun 25-Jun-13 5:50am    
Hi Shiva.,
I am not working on MVC.....i am working on asp.net webforms with entity framework and created validations using dataannotaion and was unable to display validation messages with respect to the error field....kindly provide me a solution...

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