我可以使用默认模型绑定器将多个表单绑定到单个模型吗?
我有一个复杂的页面,上面有多个表单。该页面分为几个部分,每个部分上都有一个继续按钮。
页面绑定到 pageViewModel,每个部分都处理模型上的一组不同属性。
继续按钮对控制器进行 ajax 调用,模型绑定器将其适当地绑定到模型的适当部分。该部分已适当刷新。
最后,我希望在页面底部有一个保存按钮,用于获取所有表单,并将所有表单绑定到模型。此时模型已填写所有属性,可以进行相应的处理。
我可以通过一些 ASP MVC 魔法来完成这个任务吗?
I have a complex page with several forms on it. The page is divided into sections, and each section has a continue button on it.
The page is bound to a pageViewModel, each section addresses a different set of properties on the model.
The continue button makes an ajax call to the controller, and the model binder binds it appropriately to the appropriate sections of the model. The section is refreshed appropriately.
Finally, I would like to have a save button at the bottom of the page that takes all the forms, and binds all of the forms to the model. The model, at this point has all of the properties filled out, and can be processed accordingly.
Can I accomplish this by some ASP MVC magic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASP.NET MVC Futures 包含 不错的帮助方法,允许您将模型序列化为可以在其中获取的隐藏字段表单被发布到的下一个控制器操作。这允许 WebForms ViewState 模仿。
ASP.NET MVC Futures contains nice helper method that allows you to serialize the model into a hidden field that can be fetched in the next controller action that the form is being posted to. This allows a WebForms ViewState imitation.
我会反过来做:对于这些部分,使用 jquery 仅提交该部分的元素。您已经在这里使用了ajax。过滤要发布的元素可能不是什么大问题。
这样,保存操作就不需要额外的工作。
您也可以在没有 JavaScript 的情况下让其工作。继续采取行动还需要做更多的工作,但至少是可能的。如果没有使用具有多种表单的 javascript 进行保存操作,就不可能实现此功能。
I would do it the other way round: For the sections use jquery to only submit the elements of the section. You already use ajax here. It might not be a big deal to filter the elements to post.
This way the save action would not require extra work.
You are also able to have ths working without javascript. It needs more work for the continue actions, but at least its posssible. There is no chance to get this working without javascript with multiple forms for the save action.