如何通过asp.net mvc或jquery提交包含动态数据行的表?

时间:2022-09-26 09:47:52

i'm totally noob to asp.net mvc and currently i'm writing an web app which allows user to select multiple product from a listbox and add them to a dynamic table with rows that can be added or removed.

我完全不喜欢asp.net mvc,目前我正在编写一个Web应用程序,允许用户从列表框中选择多个产品,并将它们添加到动态表中,其中包含可以添加或删除的行。

thus i'm thinking of using jquery to add and remove the table row, thus the table row will be added via append("xxx").

因此,我正在考虑使用jquery添加和删除表行,因此表行将通过append(“xxx”)添加。

but the thing is i'm not that familiar with asp.net mvc form submission, from my understanding thus far, i can retrieve the value later via form["id"] but if i were to create the table row via that method, i'm thinking it's either going to be with the same id, or i can use a running no id and a hidden input to keep track of how many products that is added.

但事情是我不熟悉asp.net mvc表单提交,从我到目前为止的理解,我可以稍后通过表单[“id”]检索值,但如果我要通过该方法创建表行,我认为它要么具有相同的ID,要么我可以使用运行无ID和隐藏输入来跟踪添加的产品数量。

also i probably want to hide the whole product table when all the products were removed.

当我删除所有产品时,我可能还想隐藏整个产品表。

p.s. kinda feel that my way is sort like a big fugly messy hack which will come back n bite me one day.

附:有点觉得我的方式有点像一个大的乱糟糟的黑客,它会回来咬我一天。

Questions:

  1. does asp.net mvc has a good way to handle all of this? i've read something about mvc tempdata.
  2. asp.net mvc有一个很好的方法来处理所有这些吗?我读过有关mvc tempdata的内容。

  3. and is there a better way for me to handle the dynamic product generation? to make things easier later for form submission in asp.net mvc
  4. 我有更好的方法来处理动态产品生成吗?以后在asp.net mvc中提交表单更容易

  5. and also i found that you can actually submit the form via jquery too, is there any pros n cons between the two method? in terms of maybe performance, security, maintenance, etc?
  6. 而且我发现你实际上也可以通过jquery提交表单,这两种方法之间有什么优点吗?在性能,安全性,维护等方面?

thanks

1 个解决方案

#1


3  

You can post collections of objects; in MVC. Here is an example: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

您可以发布对象集合;在MVC中。以下是一个示例:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Basically, you can use JQuery to create the table, but ensure the ID's of the controls reference this scheme, so for the next row to add using JQuery, ensure the controls to input the data use the next index in the list, along with the same name as the property in the underlying bound object.

基本上,您可以使用JQuery创建表,但确保控件的ID引用此方案,因此要使用JQuery添加下一行,请确保输入数据的控件使用列表中的下一个索引,以及与底层绑定对象中的属性相同的名称。

When you post to the server, you can loop through this collection.

当您发布到服务器时,您可以循环访问此集合。

To answer the others: you can submit form via JQuery if you don't want a postback to the server (can use ASP.NET MVC to do too)... otherwise, you don't need to worry about the JQuery submit. So that may add usability.

要回答其他问题:如果你不希望回发到服务器(也可以使用ASP.NET MVC),你可以通过JQuery提交表单...否则,你不需要担心JQuery提交。这样可以增加可用性。

HTH.

#1


3  

You can post collections of objects; in MVC. Here is an example: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

您可以发布对象集合;在MVC中。以下是一个示例:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Basically, you can use JQuery to create the table, but ensure the ID's of the controls reference this scheme, so for the next row to add using JQuery, ensure the controls to input the data use the next index in the list, along with the same name as the property in the underlying bound object.

基本上,您可以使用JQuery创建表,但确保控件的ID引用此方案,因此要使用JQuery添加下一行,请确保输入数据的控件使用列表中的下一个索引,以及与底层绑定对象中的属性相同的名称。

When you post to the server, you can loop through this collection.

当您发布到服务器时,您可以循环访问此集合。

To answer the others: you can submit form via JQuery if you don't want a postback to the server (can use ASP.NET MVC to do too)... otherwise, you don't need to worry about the JQuery submit. So that may add usability.

要回答其他问题:如果你不希望回发到服务器(也可以使用ASP.NET MVC),你可以通过JQuery提交表单...否则,你不需要担心JQuery提交。这样可以增加可用性。

HTH.