5

I have googled this extensively and couldn't find an answer, so I am posting this here hoping for help.

We have an asp.net 4.5 project with validation specified in the database objects classes. For example:

[Required(ErrorMessage = "Name is a required field.")]
public string Name { get; set; }

Is there any way at all that we can have this error message show up next to the field on the form page? The validation summary shows up just fine but we would really like to have them show up next to their respective fields without using the traditional, code-heavy asp.net validators (RequiredFieldValidator and others).

Thanks for the help.

EDIT: we are using webforms, not MVC

user2085436
  • 59
  • 1
  • 3

2 Answers2

2

You can use the ModelErrorMessage control. For example, let's pretend you have a model with a FirstName property. You would add something like this:

<asp:ModelErrorMessage ID="FirstNameErrorMessage" ModelStateKey="FirstName" runat="server" />
1

Unfortunately the WebForms model binding validation framework is not as comprehensive as MVC yet. For one, client side validation of data annotations is not built in. And the only way that I know of to display data annotation errors is via the ValidationSummary control, which has a ShowModelStateErrors property (default is true). Here is a super simple example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:FormView ID="FormView" runat="server" RenderOuterTable="false" ItemType="Person"
            DefaultMode="Insert" InsertMethod="FormView_InsertItem">
            <InsertItemTemplate>
                <asp:ValidationSummary ID="ValSummary" runat="server" ValidationGroup="FormGroup"
                    HeaderText="The following problems occured when submitting the form:" />
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="NameLabel" runat="server" AssociatedControlID="NameField">Name</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="NameField" runat="server" Text='<%# BindItem.Name %>' ValidationGroup="FormGroup" />
                        </td>
                    </tr>
                </table>
                <asp:Button ID="SaveButton" runat="server" CommandName="Insert" Text="Save" ValidationGroup="FormGroup" />
            </InsertItemTemplate>
        </asp:FormView>
    </form>
</body>
</html>

Code Behind:

public partial class create_person : System.Web.UI.Page
{
    public void FormView_InsertItem()
    {
        var p = new Person();
        TryUpdateModel(p);
        if (ModelState.IsValid)
        {
            Response.Write("Name: " + p.Name + "<hr />");
        }
    }
}

Class:

using System.ComponentModel.DataAnnotations;

public class Person
{
    [Required, StringLength(50)]
    public string Name { get; set; }
}

Because there is no client side / javascript support for data annotation validation, the only way to generate model state errors is after a postback. If you need inline validation next to your controls, you still need to use the standard validation controls.

I have posted a feature request on user voice asking them to consider improving the data annotation integration: http://aspnet.uservoice.com/forums/41202-asp-net-web-forms/suggestions/3534773-include-client-side-validation-when-using-data-ann

If you like the idea please vote it up on user voice. Hope this helps.

John
  • 772
  • 7
  • 17