Post an HTML Table to ADO.NET DataTable

12,230

With respect to your requirement, try this

jQuery(document).on("change", ".DDLChoices", function (e) {
    var comma_ChoiceIds = '';
    var comma_ChoicesText = '';
    $('input[class="DDLChoices"]').each(function (e) {
        if (this.checked) {
            comma_ChoiceIds = comma_ChoiceIds + $(this).val() + ',';
            comma_ChoicesText = comma_ChoicesText + $(this).parent('label').parent() + ',';
        }
    });
    $('#ChoiceIds').val(comma_ChoiceIds);
    $('#ChoiceText').val(comma_ChoicesText);
});
@using (Html.BeginForm("Actionname", "Controllername", FormMethod.Post, new { id = "frmChoices" }))
{

    @Html.HiddenFor(m => m.ChoiceText, new { @id = "ChoiceText" })
    @Html.HiddenFor(m => m.ChoiceIds, new { @id = "ChoiceIds" })
    <div class="form-group">
        <div>
            <table>
                <tr>
                    <th>Name</th>
                    <th>Selected</th>
                </tr>
                @foreach (var item in @Model.Choices)
                {
                    <tr>
                        <td> <label>@item.ChoicesText</label>    </td>
                        <td> <input class="DDLChoices" value="@item.ChoiceIds" type="checkbox" /></td>
                    </tr>
                }
            </table>
        </div>
     <input type="button" value="Submit" onclick="return ChoicesPoster.passChoices()"
    </div>
}
Share:
12,230
RKh
Author by

RKh

"I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone." - Bjarne Stroustrup

Updated on June 25, 2022

Comments

  • RKh
    RKh almost 2 years

    I have a HTML table as below in my View:

    <table id="tblCurrentYear">
        <tr>
            <td>Leave Type</td>
            <td>Leave Taken</td>
            <td>Leave Balance</td>
            <td>Leave Total</td>
        </tr>
        @foreach (var item in Model.LeaveDetailsList)
        {
            <tr>
                <td>@Html.TextBoxFor(m => item.LeaveType, new { width = "100" })</td>
                <td>@Html.TextBoxFor(m => item.LeaveTaken, new { width = "100" })</td>
                <td>@Html.TextBoxFor(m => item.LeaveBalance, new { width = "100" })</td>
                <td>@Html.TextBoxFor(m => item.LeaveTotal, new { width = "100" })</td>
            </tr>
        }
    </table>
    

    I want to iterate through all the html table rows and insert the values in ADO.NET DataTable.

    Simple speaking, converting HTML Table to ADO.NET DataTable.

    How to extract values from HTML Table and insert into ADO.NET DataTable?

    The view is based on the following model

    public class LeaveBalanceViewModel
    {
        public LeaveBalanceViewModel()
        {
            this.EmployeeDetail = new EmployeeDetails();
            this.LeaveBalanceDetail = new LeaveBalanceDetails();
            this.LeaveDetailsList = new List<LeaveBalanceDetails>();
        }
        public EmployeeDetails EmployeeDetail { get; set; }
        public LeaveBalanceDetails LeaveBalanceDetail { get; set; }
        public List<LeaveBalanceDetails> LeaveDetailsList { get; set; }
    }