Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
@model System.Data.DataTable
@{
    ViewData["Title"] = "Index1";

}

<center> <button id="button1" onclick="printDiv();" class="btn btn-success">Print</button> </center>

<button class="btn btn-primary" id="btnSaving">Save</button>


<form asp-action="Index" method="get">

    <div class="form-group">
        <label for="SearshString">Type Barcode :</label>

        <input type="text" name="searshstring" />

        <input type="submit" value="Search" class="btn btn-primary" />
    </div>
    <div id="printableTable">
            <table id="printTable" class="table table-bordered table-striped">
                <tr>
                    <th scope="col">Barcode</th>
                    <th scope="col">Itemname</th>
                    <th scope="col">SelPrice</th>
                    <th scope="colgroup">Quantity</th>
                    <th scope="colgroup">DEL</th>
                    <th scope="colgroup">ADD</th>
                    <th scope="colgroup">MINUS</th>
                </tr>
                @if (Model != null)
                {
                    @for (int i = 0; i < Model.Rows.Count; i++)
                    {
                        <tr>

                             <td>@Model.Rows[i][0]</td>
                            <td>@Model.Rows[i][1]</td>
                            <td>@Model.Rows[i][2]</td>
                            <td>1</td>
                            <td><a class="btn btn-danger" href="@Url.Action("Delete","Sales",new { @barcode=@Model.Rows[i][0]})">Delete</a></td>
                            <td><button type="button" class="btn btn-primary" onclick="addQuantity(this); ">Add Quantity</button></td>
                            <td><button type="button" class="btn btn-info" onclick="minusQuantity(this); ">Minus Quantity</button></td>

                        </tr>
                    }
                }
            </table>
        
    </div>


What I have tried:

please tell me how i send it and what is language i sent it with it
Posted

I believe you should attempt it by yourself and then share any challenges you encounter by posting a question about them.
You can achieve this task using AJAX POST method call, generate an array of objects and transfer the table rows to the ASP.NET MVC controller using jQuery AJAX function and once the response is received it is displayed using JavaScript Alert Message Box. Here are some useful links for your reference.
I want to send html table rows to ASP.NET MVC controller - Microsoft Q&A[^]
 
Share this answer
 
To send HTML table rows to an MVC controller for saving in a database, you can use JavaScript/jQuery to gather the data from the HTML table and send it to the controller using an AJAX request. Here's a basic example of how you can achieve this:

1. HTML Table Structure:
Assuming you have an HTML table structure like this:





<!-- Add more columns as needed -->






<!-- Add more cells as needed -->

<!-- Add more rows as needed -->

Column 1Column 2
Data 1Data 2


Save Data

2. JavaScript/jQuery Code:
Include jQuery in your project, and then use the following script to gather the data from the table and send it to the MVC controller:



$(document).ready(function() {
$('#saveButton').on('click', function() {
// Create an array to store the table data
var tableData = [];

// Iterate through each row in the table
$('#myTable tbody tr').each(function() {
var rowData = {};

// Iterate through each cell in the row
$(this).find('td').each(function(index, cell) {
// Get the column header for the corresponding cell
var columnHeader = $('#myTable thead th').eq(index).text();

// Add the data to the rowData object
rowData[columnHeader] = $(cell).text();
});

// Add the rowData object to the tableData array
tableData.push(rowData);
});

// Send the data to the MVC controller using AJAX
$.ajax({
url: '/ControllerName/SaveData', // Update with your actual controller and action names
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(tableData),
success: function(response) {
console.log('Data saved successfully:', response);
// Handle success as needed
},
error: function(error) {
console.error('Error saving data:', error);
// Handle error as needed
}
});
});
});


3. MVC Controller:
In your MVC controller, create an action to handle the AJAX request:
public class YourController : Controller
{
[HttpPost]
public JsonResult SaveData(List<yourmodel> tableData)
{
// Process and save the data to the database
// Make sure to replace YourModel with the actual model representing your table data

// Return a response (e.g., success message or any additional data)
return Json(new { success = true, message = "Data saved successfully" });
}
}

Make sure to replace YourModel with the actual model class that represents the data structure of your table rows.

Adjust the URLs and model structure according to your project's specifics. This example assumes a basic structure and may need modifications based on your exact use case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900