使用MVC将动态创建的表行的ID传递给控制器(passing ID of dynamically created table rows to controller using MVC)

编程入门 行业动态 更新时间:2024-10-27 09:42:37
使用MVC将动态创建的表行的ID传递给控制器(passing ID of dynamically created table rows to controller using MVC)

我有一个使用JQuery动态创建的表。 我需要将该表中每行的ID传递给控制器​​,以便将值保存到数据库。 我的动态创建的行的HTML看起来像这样

<td class="text-center" style="width:100px;" name="RegistrationTypeInspectionType.InspectionTypes" value="5"> <button class="removeInspectionType" type="button" data-val="5">Remove</button> </td>

这里是我的C#看起来像的代码片段

var registrationTypeInspectionTypes = new BusinessLayer.RegistrationTypeInspectionType().GetAll(); model.RegistrationTypeInspectionType.RegistrationTypeInspectionTypes = registrationTypeInspectionTypes;

当我提交表单时,这些值不会被发送给控制器。

I have a table that is dynamically created using JQuery. I need to pass the IDs of every row in that table to the controller so that I can save the values to the Database. My HTML for the dynamically created rows looks like this

<td class="text-center" style="width:100px;" name="RegistrationTypeInspectionType.InspectionTypes" value="5"> <button class="removeInspectionType" type="button" data-val="5">Remove</button> </td>

here is a code snippet of what my C# looks like

var registrationTypeInspectionTypes = new BusinessLayer.RegistrationTypeInspectionType().GetAll(); model.RegistrationTypeInspectionType.RegistrationTypeInspectionTypes = registrationTypeInspectionTypes;

when I submit the form these values are not being sent to the controller.

最满意答案

只有输入字段不是表格的td或tr。 请尝试在每个td中使用要提交的名称创建隐藏字段。 你的桌子应该看起来像这样

<td class="text-center" style="width:100px;" name="RegistrationTypeInspectionType.InspectionTypes" value="5"> <input type="hidden" name="RegistrationTypeInspectionType.InspectionTypes" value="5"/> <button class="removeInspectionType" type="button" data-val="5">Remove</button>

现在,当您提交表单时,这些隐藏的字段也将被提交,您可以在控制器中获取这些字段。

Only input fields are submitted not the td or tr of the table. Please try to create hidden fields in each td with the name you want to submit. your table should look like this

<td class="text-center" style="width:100px;" name="RegistrationTypeInspectionType.InspectionTypes" value="5"> <input type="hidden" name="RegistrationTypeInspectionType.InspectionTypes" value="5"/> <button class="removeInspectionType" type="button" data-val="5">Remove</button>

Now when you submit the form these hidden fields will also be submitted and you can get these in the controller.

更多推荐

registrationTypeInspectionTypes,RegistrationTypeInspectionType,class,电脑培训,计算机培训,

本文发布于:2023-07-30 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   动态   passing   MVC   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!