且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 ASP.Net MVC 5 中动态添加新行

更新时间:2023-12-06 13:56:28

我不会通过以您描述的方式修改 dom 来动态执行此类操作.我的偏好是在 razor 视图中生成所有必要的代码,就好像它总是在那里一样,然后简单地切换行本身的可见性.这样,在生成视图时,文本框会正确呈现为表单元素,并且您仍然可以完全访问使用 jQuery 修改表,以等待任何 AJAX 请求.

I would not do this sort of thing dynamically by modifying the dom in the manner you describe. My preference would be to generate all of the necessary code in the razor view as if it was always there and then simply toggle the visibility of the row itself. This way the textbox is rendered properly as a form element when the view is generated, and you still have full access to modify the table with jQuery pending any AJAX requests.

顺便说一句,您所描述的行为听起来像是您在尝试添加更多客户端行为并减少到服务器的往返次数/大小.如果这是真的,我建议探索 JavaScript MVVM 框架,如 Knockout、Ember 或 Angular.

On a side note, the behavior you're describing makes it sound like you're attempting to add more client side behaviors and reduce the number/size of the round trips to the server. If this is true, I would suggest exploring a JavaScript MVVM framework like Knockout, Ember or Angular.