aspnet / AspNetWebStack

ASP.NET MVC 5.x, Web API 2.x, and Web Pages 3.x (not ASP.NET Core)
Other
858 stars 354 forks source link

Server side validation message does not show inside a PartialView #304

Closed mkArtakMSFT closed 3 years ago

mkArtakMSFT commented 3 years ago

Issue moved from dotnet/aspnetcore#35903


From @HoomanBahreini on Sunday, August 29, 2021 5:57:00 AM

I believe this is a bug, with ASP.NET MVC 5, I am not sure where should I report MVC 5 bugs? I have not tested this with .NET core.


I have created a custom Validation attribute, to check PhoneNumbers:

public class PhoneNumberIsValidAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value != null)
        {
            string phoneNumberRaw = (string)value;
            Regex regex = new Regex(@"^\d{6,8}$");
            Match match = regex.Match(phoneNumberRaw);
            if (match.Success)
            {
                return true;
            }
        }

        return false;
    }
}

I annotate my model:

public class MyModel
{   
    [DisplayName("Override phone number")]
    [PhoneNumberIsValid("Test")]
    public string OverridePhoneNumber { get; set; }
}

And in myView:

@Html.ValidationSummary(false, "", new { @class = "text-danger" })

<div class="form-group">
    @Html.LabelFor(model => model.OverridePhoneNumber)
    @Html.TextBoxFor(model => model.OverridePhoneNumber, new { @class = "form-control input-text", @maxlength = GlobalConstants.MaxLengthForPhoneNumber /*, @type = "url" */})
    @Html.ValidationMessageFor(model => model.OverridePhoneNumber, "", new { @class = "text-danger" })
</div>

Everything works as expected, as demonstrated in this fiddle

Here is the problem

And I cannot demonstrate the issue in the fiddle.

If I put the input inside _MyPartialView, and call it from MyView as shown below:

@Html.Partial("path/_MyPartialView", Model)

Then the ValidationMessage does not show under the input box as shown in the snippet below:

enter image description here

This problem only happen when:

  1. The input is inside a PartialView
  2. When the error message comes from the Server side (Client side errors messages are generated correctly)

I got this error with both .NET Framework 4.6.1 and 4.7.2. Here is my ASP.NET MVC version: enter image description here

mkArtakMSFT commented 3 years ago

Issue moved from dotnet/aspnetcore#35903


From @mkArtakMSFT on Monday, August 30, 2021 4:49:23 PM

Thanks for contacting us. ASP.NET MVC 5 is in maintenance mode and we make only critical bug fixes on it.

mkArtakMSFT commented 3 years ago

Thanks for contacting us. We're not making any improvements in this area any more as this project is in maintenance mode. Only critical blocking issue with wide impact and security issues are considered. This one doesn't seem as critical given that we haven't received many reports from customers.