Feature Question/Request: Razor Pages free view model property annotations
This isn't (probably) a thing, but I tried it because it would be cool if it were an actual feature.
Of course, old skool annotations work fine with a data-bound class.
For the free view model that comes with a Razor Page, I wish I could just do this ...
<form method="post" enctype="multipart/form-data">
<p>
<input asp-for="Query" type="text">
<span asp-validation-for="Query"></span>
<button type="submit">Run Query</button>
</p>
</form>
... with (in the public class IndexModel : PageModel) ...
[BindProperty, Required, StringLength(50)]
public string Query { get; set; }
Is there an MVC theory that argues against this pattern? ... or is it just not a feature? ... If not, would u consider offering it?
Note: I couldn't get remote validation to work either on a Razor Pages free view model prop.
We plan to enable things like this in 2.1. This isn't possible today due to some limitations in the validation system.
@rynowak - do you know what issue (if any) is tracking this work for 2.1? I want to make sure this doesn't get lost.
https://github.com/aspnet/Mvc/issues/6790 tracks this work
FWIW, I just hit this too, and just assumed that it would work this way.
It seems you can bind properties, but validation doesn't work?
MVC has never supported validation attributes on 'top-level' items (parameters, properties). This was less of a problem before we did property binding on controllers (and then pages).
We're adding this in 2.1 because it's an obvious gap.
To my knowledge, this issue didn't make it to 2.1 -- rynowak said Issue #6790 tracks this work and it was scheduled for 2.1.0 preview 1 but I don't see it in the change log for 2.1.0 (https://github.com/aspnet/Home/releases/tag/2.1.0)
Can someone confirm? Would be nice to get validation working on bound properties in the PageModel class.
The linked issue https://github.com/aspnet/Mvc/issues/6790 was fixed in 2.1.0-preview1. The support for validation attributes on Razor Page model properties and parameters was added in 2.1.0-preview2 (See https://github.com/aspnet/Mvc/issues/6790). If you're seeing issues with it, please file a new issue in https://github.com/aspnet/Mvc/issues
@pranavkm -- Sorry I'm not seeing it. You said to refer to #6790 to see that it was added in preview2 -- but I don't see preview2 anywhere on that issue.
In my testing, this works as expected in 2.1
@ctolkien @pranavkm -- Ok it does seem to work; I have created a fresh AspNetCore 2.1 project to test and it's functioning as expected. In the web application that I'm having trouble with I upgraded to 2.1 from 2.0 and it's still exhibiting issues so I'm not sure if it's for some reason using any 2.0 libraries still or not. Anyone else having issues when upgrading an app from 2.0 to 2.1?
Edit: Yep -- there seems to be an issue when you start with a 2.0 application, then update it to 2.1 (updating the framework first, then updating any nuget packages). The validation still seems to use 2.0 for some reason. Am I missing something in my upgrade process?
Found it!
Well stumbling onto the solution, I had to replace the nuget package Microsoft.AspNetCore.All 2.1.0 with Microsoft.AspNetCore.App 2.1.0, then it seems like you need to set the MVC compatibility version in ConfigureServices in the Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
to
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Hope this helps others.