简体   繁体   中英

ASP.NET Razor - Binding multiple properties and forms in one PageModel

I'm very new to working in Microsoft Visual Studio and I set up a new Razor Web Application following the documentation and now I want to combine some of the actions that are broken apart on several pages.

For instance I want to use on PageModel for a "Sign Up" view, a "Sign In" view and a "Forgot Password" view but when trying to combine all "OnPostAsync" methods (using page-handlers and different names) the forms interfere with each other and the ModelState is always invalid.

I am also trying to combine the "Change Password" view with the "Update Profile" view and again, I am having the same issue.

Is it not possible to achieve something like this using Razor? It doesn't seem likely for that to be true.

It is possible to post multiple different forms to the same page, but it is not recommended unless they all share the same model. Otherwise, as you seem to have found, things like a property which is [Required] for one form but not another will cause validation problems.

If you have to start compromising on the design of your PageModel to accommodate multiple forms, that should be a sign that you really need separate pages for the forms. Any benefit you might gain by reducing duplication in UI is being lost through additional complexity with validation etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM