[英]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.我对在 Microsoft Visual Studio 中工作非常陌生,我按照文档设置了一个新的 Razor Web 应用程序,现在我想合并一些在多个页面上分开的操作。
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.例如,我想在 PageModel 上使用“注册”视图、“登录”视图和“忘记密码”视图,但是当尝试组合所有“OnPostAsync”方法(使用页面处理程序和不同名称)时,表单相互干扰,ModelState 始终无效。
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?使用 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.否则,正如您似乎已经发现的那样,像一个表单
[Required]
而不是另一个表单的属性会导致验证问题。
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.如果您必须开始在 PageModel 的设计上妥协以容纳多个表单,这应该表明您确实需要为表单单独的页面。 Any benefit you might gain by reducing duplication in UI is being lost through additional complexity with validation etc.
通过减少 UI 中的重复可能获得的任何好处都将因验证等的额外复杂性而丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.