如何使用jquery / asp.net mvc在表中发布动态创建的字段

时间:2022-12-01 08:16:16

I have a button that will create a new entry (row in a table) dynamically using jQuery. Each entry is a row in a html table.

我有一个按钮,将使用jQuery动态创建一个新条目(表中的行)。每个条目都是html表中的一行。

For each row, each column has an input (textbox, radio button, checkbox) etc.

对于每一行,每列都有一个输入(文本框,单选按钮,复选框)等。

Because you can add as many rows as you like you end up with some array or collection of these rows.

因为您可以添加任意数量的行,所以最终会得到一些数组或这些行的集合。

When I post the form, I don't seem to see this data in the formscollection and don't quite understand how to translate these controls into a data object for binding.

当我发布表单时,我似乎没有在formscollection中看到这些数据,也不太了解如何将这些控件转换为数据对象以进行绑定。

So essentially there is 2 questions:

所以基本上有两个问题:

  1. Is there any issue with dynamically created controls and making sure they show up in the form post?

    动态创建的控件是否存在任何问题,并确保它们显示在表单中?

  2. What is a way to pass along a table structure of data to my controller. I almost want to have each row represent some Record Object and then pass over a collection of records to the controller if something like that is possible.

    什么是将数据表结构传递给控制器​​的方法。我几乎想让每一行代表一些记录对象,然后将一组记录传递给控制器​​,如果可能的话。

Any suggestions?

3 个解决方案

#1


1  

To get a collection, make sure the name of the textboxes is something like "MyRecord.MyList[0].Field1". MVC will automatically pull that up to an enumerable. For your javascript adds, just make sure each added row has a properly incremented index (eg, new fields named "MyRecord.MyList[1].Field1").

要获取集合,请确保文本框的名称类似于“MyRecord.MyList [0] .Field1”。 MVC会自动将其拉到可枚举的位置。对于你的javascript添加,只需确保每个添加的行都有一个正确递增的索引(例如,名为“MyRecord.MyList [1] .Field1”的新字段)。

#2


0  

You do need to make sure your dynamically created elements are between the form tags.

您需要确保动态创建的元素位于表单标记之间。

The default model binding will bind to an array of objects if you make sure to name your fields in such a way that they become 'Thing[3].Name' (this is the Name field in the fourth row, zero bound array)

如果确保将字段命名为“Thing [3] .Name”(这是第四行中的Name字段,零绑定数组),则默认模型绑定将绑定到对象数组

Your signatture for your Action method then becomes some like:

您对Action方法的签名会变得像:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MethodName(thing[] things)
{
    ...  
}

Kindness,

Dan

#3


0  

Make sure each row's ID is unique, but it should post back if within the form that's posting to the server... How are you doing the post? Are you doing the post with JQuery?

确保每一行的ID都是唯一的,但如果在发布到服务器的表单中,它应该回发...你是如何做这个帖子的?你在用JQuery做这个帖子吗?

#1


1  

To get a collection, make sure the name of the textboxes is something like "MyRecord.MyList[0].Field1". MVC will automatically pull that up to an enumerable. For your javascript adds, just make sure each added row has a properly incremented index (eg, new fields named "MyRecord.MyList[1].Field1").

要获取集合,请确保文本框的名称类似于“MyRecord.MyList [0] .Field1”。 MVC会自动将其拉到可枚举的位置。对于你的javascript添加,只需确保每个添加的行都有一个正确递增的索引(例如,名为“MyRecord.MyList [1] .Field1”的新字段)。

#2


0  

You do need to make sure your dynamically created elements are between the form tags.

您需要确保动态创建的元素位于表单标记之间。

The default model binding will bind to an array of objects if you make sure to name your fields in such a way that they become 'Thing[3].Name' (this is the Name field in the fourth row, zero bound array)

如果确保将字段命名为“Thing [3] .Name”(这是第四行中的Name字段,零绑定数组),则默认模型绑定将绑定到对象数组

Your signatture for your Action method then becomes some like:

您对Action方法的签名会变得像:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MethodName(thing[] things)
{
    ...  
}

Kindness,

Dan

#3


0  

Make sure each row's ID is unique, but it should post back if within the form that's posting to the server... How are you doing the post? Are you doing the post with JQuery?

确保每一行的ID都是唯一的,但如果在发布到服务器的表单中,它应该回发...你是如何做这个帖子的?你在用JQuery做这个帖子吗?