aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

CompareAttribute not working properly with bound properties on the Razor Page's PageModel

Open RKDMax opened this issue 8 years ago • 25 comments

Is this a Bug or Feature request?:

Bug

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

See This Repo to reproduce

Description of the problem:

When using a compare attribute on bound properties in the PageModel itself, the otherProperty parameter is not getting honoured and results in a "Could not find a property named <otherProperty>".

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

Microsoft.AspNetCore.App 2.1.0

RKDMax avatar Jun 14 '18 19:06 RKDMax

Thanks for contacting us, @shanselman. @dougbu, can you please look into this? Thanks!

mkArtakMSFT avatar Jun 15 '18 16:06 mkArtakMSFT

The difference between ThisDoesNotWorkModel and ThisWorksModel is the validation level at which the CompareAttribute is encountered. ValidationContext.ObjectType is typeof(string) instead of typeof(ThisDoesNotWorkModel) in your failing case because ModelValidationContext.Container is null for all top-level models i.e. controller or page model properties and action or handler parameters.

The workaround is exactly what you did in ThisWorksModel -- use [Compare] on a wrapper type instead of top-level models.

dougbu avatar Jun 15 '18 22:06 dougbu

@danroth27 @mkArtakMSFT @rynowak the new support for validation of top-level nodes does not pass the containing page model to ValidationAttributes, breaking [Compare] on page model properties. In addition, page model implementations of IValidatableObject are ignored. This is not what users should expect.

Though MVC passes an incorrectly-initialized ValidationContext for the page model property case, it's difficult to call this a bug since a fix enables new scenarios. Suggest we treat this as an enhancement for a near-term release. One possibility would be to validate the page model itself, passing a custom IValidationStrategy to ignore properties that were not bound. (Could also add a ValidationVisitor.Validate(...) overload accepting the container object and use it for page model property validation. That would address @RKDMax's scenario but not get page model IValidatableObject implementations working.)

dougbu avatar Jun 16 '18 22:06 dougbu

Cleared investigate label and my assignment. Over to you Triage team

dougbu avatar Jun 16 '18 22:06 dougbu

Closing this issue as there was no community involvement for quite a while now.

mkArtakMSFT avatar Jan 15 '19 00:01 mkArtakMSFT

I’m hitting this problem now too, with properties in my PageModel that are bound directly using [BindProperty].

I do not like the workaround of having to create a sub-model for the form since this means that the form field names are unnecessarily nested. An easier workaround would be to simply implement the comparison check in code then since that means that you wouldn’t have to update your model.

I do consider this a bug since all validation attributes do work except for [Compare]. Combined with the error which isn’t clear on this restriction at all, this makes this also very difficult to discover. I would assume that most people would just expect this to work out of the box.

poke avatar Mar 28 '19 13:03 poke

Microsoft.AspNetCore.App 2.2.0 Bug is presented

CaCTuCaTu4ECKuu avatar Apr 25 '19 07:04 CaCTuCaTu4ECKuu

I have also just encountered this problem. To me the workaround kind of defeats one the purposes of having the PageModel i.e It is the page's model. I should not need a PageModel-model. Additionally, the other validators work. It is surprising to suddenly discover that Compare did not.

steveofficer avatar May 04 '19 09:05 steveofficer

In ASP.Net Core 2.2:

    public class RegisterModel : PageModel
	{
		public RegisterModel(UserManager<VocalsUser> userManager, ICaptchaService captchaService, IEmailSender emailSender)
		{
			UserManager = userManager;
			CaptchaService = captchaService;
			EmailSender = emailSender;
		}

		[Required]
		[BindProperty]
		[DataType(DataType.Text)]
		public string UserName { get; set; }

		[Required]
		[BindProperty]
		[DataType(DataType.EmailAddress)]
		public string Email { get; set; }

		[Required]
		[BindProperty]
		[DataType(DataType.Password)]
		public string Password { get; set; }

		[Required]
		[BindProperty]
		[DataType(DataType.Password)]
		[Compare(nameof(Password))]
		public string PasswordRepeated { get; set; }

CompareAttribute works nicely in view (i.e. with unobtrusive validation) but causes invalid ModelState to occur in the OnPost method. I'm effectively forced between doing either manual validation in the view without [Compare], or manually clearing the ModelState error in the OnPost method. That doesn't sound right, especially when following Razor PageModel examples & tutorials like this one that list the [Compare] attribute as a viable validation technique for Razor pages.

JohannSig avatar May 06 '19 23:05 JohannSig

I'd have to agree with @JohannSig. I shouldn't need another model class inside my PageModel simply to use the [Compare] attribute functionality. One of the benefits of Razor Pages is combining the model and the controller for simple pages to reduce code size, but if I have to create another model for my PageModel to use, it negates this benefit and makes me wondering if I should just go back to MVC.

ArcanoxDragon avatar May 20 '19 03:05 ArcanoxDragon

Just started out with Core and encountered this inexplicably dumb oversight. It makes absolutely no sense to respect some validation attributes and not others- especially IValidateableObject. In addition, I agree with others that the nested class solution is bonkers and defeats the purpose of PageModel.

cmoya212 avatar May 23 '19 23:05 cmoya212

Totally agree! On .NET Core using [Compare] MVC works, but not on Pagemodel! I had to change the code and implement IValidatableObject and validate it explicitly. Clumsy code :(

egbertn avatar May 28 '19 06:05 egbertn

I think it would go a long way for the New Page template to automatically create the “InputModel” nested class inside of PageModel. You can always delete it if your page has no postbacks. Otherwise, this is highly unintuitive.

cmoya212 avatar May 28 '19 07:05 cmoya212

Unsure what being closed means here - will this ever be fixed?

Haukinger avatar Jul 11 '19 15:07 Haukinger

Reopening as it seems there is enough involvement here to reconsider this.

mkArtakMSFT avatar Jul 11 '19 17:07 mkArtakMSFT

hope the issue be resolved as soon as possible!

freeflying1222 avatar Jul 12 '19 07:07 freeflying1222

Having same problem, shame it's not in 3.1.0-preview1.

edgaras-civinskas avatar Oct 16 '19 11:10 edgaras-civinskas

Was just bitten by this, adding my +1

ctolkien avatar Oct 21 '19 21:10 ctolkien

@ctolkien Me too :(

jomeno avatar Nov 11 '19 08:11 jomeno

Happy Holidays! #metoo

jfaquinojr avatar Dec 18 '19 11:12 jfaquinojr