MVC 中具有回发功能的动态可编辑表/网格生成
我需要在 MVC 中生成一个表,该表可以具有一组可变的水平列(年份)。我需要在每个单元格中渲染一个文本框,并且需要将值回发到操作方法。我见过生成可编辑单元格但列是固定的(使用部分)的示例。我还看到了可以使用动态列呈现表格但没有可编辑单元格/文本框的示例。谁能建议一种方法?
I need to generate a table in MVC that can have a variable set of horizontal columns (years). I need to render a textbox in each cell and I need to postback the values to a action method. I have seen examples where the editable cells are generated but the columns are fixed (using partials). I have also seen examples where the table can be rendered with dynamic columns but without the editable cells/textboxes. Can anyone suggest an approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议创建动态表,每个单元格中都有一个文本框,并使用 onchange 操作通过 ajax 将数据发送到控制器进行更新。
您可能需要在模型中传递一个多维数组并使用它来创建和加载表。
I would recommend creating the dynamic table with a textbox in each cell with an onchange action to send the data via ajax to the controller for the update.
You will probably need to pass a multidimensional array within the model and use it to create and load your table.
问题是您希望如何在服务器端处理这个问题?
如果您按顺序命名它们并提前知道列数,则模型绑定器可以为您绑定到一个列表(如果它们都以适当的格式命名)。您想从模型或其他方法生成列表吗?
Phil Haack 介绍了命名格式,尽管 EditorFor 在某些情况下会自动处理。如果它在您的系统中不起作用,只需在此方案中命名它们就可以了。
http://haacked.com/archive/2008 /10/23/model-binding-to-a-list.aspx
The question is though how are you expecting to handle this on the server side?
If you name them all sequentially and know the # of columns ahead of time the model binder CAN bind to a list for you if they are all named in the appropriate format. Do you want to generate the list from a model or some other method?
Phil Haack covers how the naming format is, although the EditorFor will handle this automatically in some cases. If it doesnt work in yours, simply naming them in this scheme should work.
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx