简体   繁体   English

ASP.NET Razor - 在一个 PageModel 中绑定多个属性和表单

[英]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.

相关问题 简单的注入器注入PageModel ASP.NET核心Razor页面 - Simple Injector Inject into PageModel ASP.NET Core Razor Pages 有没有什么简单的方法可以将复杂对象从一个 PageModel 文件传递​​到 asp.net 核心剃刀页面(mvvm,而不是 mvc)中的另一个? - Is there any easy way to to pass complex objects from one PageModel file to another in asp.net core razor pages (mvvm, not mvc)? C# ASP.NET 核心 Razor 页面 - 从 Razor 页面内部调用 PageModel 方法? - C# ASP.NET Core Razor page - call PageModel method from inside the Razor page? 如何在 PageModel 中为表单分配属性(按按钮),维护选定的页面布局 [ASP.NET/Razor 页面] - How to assign property in PageModel (by button) for form, maintaining selected page layout [ASP.NET/Razor Page] ASP.NET 内核 Razor PageModel ModelState 无效,因为新的导航属性 Id==0 - ASP.NET Core Razor PageModel ModelState Invalid because of new Navigation Property with Id==0 使用ASP.NET Razor绑定列表列表 - Binding List of Lists with ASP.NET Razor ASP.NET MVC Razor 中的嵌套绑定 - Nested binding in ASP.NET MVC Razor 在一个 razor 视图中创建多个模型(ASP.NET Core) - Multiple create models in one razor view (ASP.NET Core) ASP.NET 核心 Razor 页面绑定属性和保存数据 model - ASP.NET Core Razor Page binding properties and preserve data in model 在 ASP.NET Razor 中动态访问属性 - Dynamically Access Properties in ASP.NET Razor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM