如何在 ASP.NET MVC 中定义表单字段前缀
我正在尝试在 ASP.NET MVC 中呈现复合 ProductCatalog
视图。这要求我在每个页面呈现多个 Product
视图。每个产品视图都是一个单独的表单。我需要表单字段具有基于 ID 的前缀,这样渲染的文档中就不会出现重复的 ID。有没有办法定义一个前缀应用于由 Html 扩展生成的所有表单字段,或者我是否需要手动构建它?
I am attempting to render a composite ProductCatalog
view in ASP.NET MVC. This requires that I render multiple Product
views per page. Each product view is a separate form. I need the form fields to have a prefix based on the id, so that I don't have duplicate IDs in the rendered document. Is there a way to define a prefix to be applied to all the form fields that are generated by the Html Extensions, or do I need to build this out manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以根据执行操作为视图内的控件定义前缀,请考虑应放置在 GET 操作方法中的以下代码:
这将为您的视图控件添加所需的前缀,但为了处理当您发布页面时,您将需要重新定义 POST 操作签名中的前缀,如下所示:
让我知道它是否有效,谢谢。
Yes, you can define a prefix for the controls inside your view based on the executing action, consider the following code that should be place in your GET action method:
this will add the required prefix to your View controls, but in order to deal with them back when you post your page, you will need to redefine the prefix in the signature of your POST action as the following:
Let me know if it worked, thanks.