Home >Backend Development >C++ >How to Correctly Map HTML Table Data to an ADO.NET DataTable via Model Binding?

How to Correctly Map HTML Table Data to an ADO.NET DataTable via Model Binding?

Susan Sarandon
Susan SarandonOriginal
2025-02-03 08:01:10908browse

How to Correctly Map HTML Table Data to an ADO.NET DataTable via Model Binding?

Convert the html table to Ado.net Datatable

Publish the HTML table value to Ado.net Datatable. The key is to ensure that the HTML control name corresponds to the model attribute.

Correct HTML control to submit the form

The control name generated by the current HTML labeling is incorrect, which hinders the data binding with the model. In order to correctly bind, the control name must be aligned with the attributes of the model. For the first line, you should not use "item.leaveType", but "LeaveDetailsList [0] .leaveType" should be used. For the second line, "LeaveDetailsList [1] .leaveType" should be used.

Modify the html code

In order to achieve this, if the collective is achieved, you can use the for loop, or to realize the collection of EditortEmplate in order to achieve the collection of INUMERABLE .

for loop method:

Editortemplate method:

Create /Views/shaRd/editortEmplates/leavebalanceDetails.cshtml:
<code class="language-csharp">for (int i = 0; i < ...; i++) {
    // 使用 LeaveDetailsList[i].LeaveType 等生成控件
}</code>

In the main view:

<制> The controller code

<code class="language-csharp">@model yourAssembly.LeaveBalanceDetails
<tr>
    <td>@Html.TextBoxFor(m => m.LeaveType)</td>
    ....
</tr></code>

Finally, in the Edit method of the controller:

<code class="language-csharp"><table>
    .... // 添加表头
    <tbody>
        @Html.EditorFor(m => m.LeaveDetailsList)
    </tbody>
</table></code>

The above is the detailed content of How to Correctly Map HTML Table Data to an ADO.NET DataTable via Model Binding?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn