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

发布于 2024-08-16 05:01:19 字数 388 浏览 9 评论 0原文

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

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

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

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

所以本质上有两个问题:

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

  2. 将数据表结构传递到我的控制器的方法是什么?我几乎想让每一行代表一些记录对象,然后将记录集合传递到控制器(如果可能的话)。

有什么建议吗?

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-08-23 05:01:19

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

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").

∞觅青森が 2024-08-23 05:01:19

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

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

如果您 你的 Action 方法的签名就会变得像:

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

Kindness,

Dan

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)

Your signatture for your Action method then becomes some like:

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

Kindness,

Dan

人心善变 2024-08-23 05:01:19

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文