validation summary in mvc 4.0

2019-09-16 04:20发布

问题:

I am new to mvc I am trying simple validation summary but the validation summary is not showing in UI.IS there any syntax wrong.

   @{Html.ValidationSummary();}
    @{Html.BeginForm();}

    <p>
        Name:-@Html.TextBox("Name")
    </p>
    <p>
        Age:-@Html.TextBox("Age")
    </p>
    <input type="submit" value="Sign In" />
    <h6>SignIn</h6>
    @{Html.EndForm();}

回答1:

This should work:

@using (Html.BeginForm("YourActionName", "YourControllerName","Upload", FormMethod.Post)) {
    @Html.ValidationSummary(false);
    <p>
        Name: @Html.TextBoxFor(m => m.Name)
    </p>
    <p>
        Age: @Html.TextBoxFor(m => m.Age)
    </p>
    <input type="submit" value="Sign In" />
    <h6>SignIn</h6>
}

...Assuming that you have validation attributes (e.g. [Required]) on the properties on the model.

Notice that the validation summary is inside the Html.BeginForm() block.

Also notice the TextboxFor syntax (rather than Textbox) which makes sure that your properties are strongly typed.

Also, make sure you have these settings in the appsettings section of your web.config file:

<add key="ClientValidationEnabled" value="true"></add>
<add key="UnobtrusiveJavaScriptEnabled" value="true"></add>