Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does @Html.ValidationSummary work for client side validation in MVC3 or MVC4?

I have the following code:

@Html.ValidationSummary(false) 
@Html.ValidationSummary(true, "xxx")

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>

When I enter an Order value of 999 then I immediately get client side validation that shows me an error message just after the Order Text box. However nothing shows up in the Html.ValidationSummary area. Is it possible to use this with client side validation?

Here is my model:

public class Menu
{
    [Range(0, 99, ErrorMessage = "{0} must be between {1} and {2}")]
    [DisplayName("Order")]
    public int Order { get; set; }

My web config:

  <appSettings>
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
like image 996
Samantha J T Star Avatar asked Oct 08 '22 18:10

Samantha J T Star


1 Answers

I'm not 100% certain but the validation summary I can find in the MVC 3 default application is within the Form. Maybe because your validation summaries are sitting outside of the form they don't get updated. What happens when you move the Validation summary inside the form?

From:

@Html.ValidationSummary(false) 
@Html.ValidationSummary(true, "xxx")

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>

To:

@using (Ajax.BeginForm(
        action,
        "Menus",
        null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = success
        }, new { @id = "menuForm" }))
{
    @Html.ValidationSummary(false) 
    @Html.ValidationSummary(true, "xxx")
    <dl>
    <dt>@Html.LabelFor(model => model.Order)</dt>
    <dd>@Html.TextBoxFor(model => model.Order)</dd>
    <dd>@Html.ValidationMessageFor(model => model.Order)</dd>
    </dl>
like image 170
Nope Avatar answered Oct 12 '22 05:10

Nope