Validation errors in asp.net 4.5

I strongly searched for this information and could not find an answer, so I post it here, hoping for help.

We have an asp.net 4.5 project with validation defined in the database object classes. For instance:

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

Is there any way to show the error message next to the field on the form page? The validation summary looks just fine, but we would really like it to be displayed next to their respective fields, without using traditional, reliable asp.net authentication (RequiredFieldValidator and others).

Thanks for the help.

EDIT: we use webforms, not MVC

+5
source share
2 answers

ModelErrorMessage. , , FirstName. - :

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

, WebForms , MVC. -, . , , , - ValidationSummary, ShowModelStateErrors ( ). :

 
<!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>

:

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 />");
        }
    }
}

:

using System.ComponentModel.DataAnnotations;

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

/javascript , - . , .

, : http://aspnet.uservoice.com/forums/41202-asp-net-web-forms/suggestions/3534773-include-client-side-validation-when-using-data-ann

, . , .

+1

All Articles