How to passing data parameter into controller in C# asp.net core ?

How to passing data parameter into controller in C# asp.net core ?

jigujigujigujigu Posts: 3Questions: 2Answers: 0

Hi All,

I just learn c# asp.net core and datatables.

How to passing data parameter into controller in asp.net core ?
i don't know any ide to catch the data/value from client-side.

below my model, controller and index.cshtml.

My Class :

    public class Customer
    {
        [Key]
        public int id { get; set; }
        public string customerCode { get; set; }
        public string companyName { get; set; }
        public string contactName { get; set; }
        public string contactTitle { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string region { get; set; }
        public string postalCode { get; set; }
        public string country { get; set; }
        public string phone { get; set; }
        public string fax { get; set; }
    }

My Controller :

        [HttpPost]
        public async Task<IActionResult> OperationData([FromForm] string action, List<string> data)   <<--- is this correct ??
        {

            if (action == "remove")
            {
                var customer = await _context.Customers.SingleOrDefaultAsync(m => m.id == data.????);   <<--- how to delete ??
                _context.Customers.Remove(customer);
                await _context.SaveChangesAsync();

            }
            return RedirectToAction("Index");

        }

My Index.cshtml

> @{
>     ViewData["Title"] = "Customer";
> }
> <h2>@ViewData["Title"].</h2>
> <h3>@ViewData["Message"]</h3>
> 
> <div style="width:90%; margin:0 auto;">
>     <button id="button" />
> 
>     <table id="myTable">
>         <thead>
>             <tr>
>                 <th></th>
>                 <th>Code</th>
>                 <th>Contact Name</th>
>                 <th>Company</th>
>                 <th>Phone</th>
>                 <th>Country</th>
>                 <th>City</th>
>                 @* <th>Postal Code</th>*@
>             </tr>
>         </thead>
> 
>     </table>
> 
> </div>
> 
> <style>
>     tr.even {
>         background-color: #F5F5F5 !important;
>     }
> </style>
> @* Load datatable css *@
> <link href="~/lib/datatables/css/jquery.dataTables.min.css" rel="stylesheet" />
> <link href="~/lib/datatables/css/buttons.dataTables.min.css" rel="stylesheet" />
> <link href="~/lib/datatables/css/select.dataTables.min.css" rel="stylesheet" />
> <link href="~/lib/datatables/css/editor.dataTables.min.css" rel="stylesheet" />
> 
> 
> 
> @* Load datatable js *@
> @section Scripts{
>     <script src="~/lib/datatables/js/jquery.dataTables.min.js"></script>
>     <script src="~/lib/datatables/js/dataTables.buttons.min.js"></script>
>     <script src="~/lib/datatables/js/dataTables.select.min.js"></script>
>     <script src="~/lib/datatables/js/dataTables.editor.js"></script>
> 
> 
>     <script>
>         var editor;
>         $(document).ready(function () {
> 
> 
>             editor = new $.fn.dataTable.Editor({
>                 "ajax": "/custs/OperationData",
>                 "data" : "test",
>                 "table": "#myTable",
>                 "idSrc": "id",
>                 "fields": [{
>                     label: "Customer Code:",
>                     name: "customerCode"
>                 }, {
>                     label: "Contact Name:",
>                     name: "contactName"
>                 }, {
>                     label: "Company Name:",
>                     name: "companyName"
>                 }, {
>                     label: "Phone:",
>                     name: "phone"
>                 }, {
>                     label: "Country:",
>                     name: "country"
>                 }, {
>                     label: "City:",
>                     name: "city"
>                 }
>                 ]
>             });
> 
> 
>             $('#myTable').on('click', 'tbody td:not(:first-child)', function (e) {
>                 editor.inline(this);
>             });
> 
> 
> 
>             var table = $('#myTable').DataTable({
>                 "dom": "Bfrtip",
>                 "ajax": {
>                     "url": "/custs/loaddata",
>                     "type": "GET",
>                     "datatype": "json"
> 
>                 },
> 
>                 "columns": [
>                         {
>                             data: null,
>                             defaultContent: '',
>                             className: 'select-checkbox',
>                             orderable: false
>                         },
> 
>                         { "data": "customerCode", "autoWidth":true},
>                         { "data": "contactName", "autoWidth": true },
>                         { "data": "companyName", "autoWidth": true },
>                         { "data": "phone", "autoWidth": true },
>                         { "data": "country", "autoWidth": true },
>                         { "data": "city", "autoWidth": true },
> 
>                         //{ "data": "PostalCode", "autoWidth": true }
>                 ],
>                 "order": [[0, "asc"]],
> 
>                 select: {
>                 style:    'os',
>                 selector: 'td:first-child'
>                 },
> 
>                 buttons: [
>                     { extend: "create", editor: editor },
>                     { extend: "edit",   editor: editor },
>                     { extend: "remove", editor: editor }
>                 ]
> 
> 
>              });
>          });
>     </script>
> }
> 

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Answer ✓

    You can probably get the properties from HttpContext.Current.Request. Or if you are using the Editor .NET libraries (which it doesn't look like you are) you could just pass that Request object into its Process() method and it will handle the delete for you.

    Allan

  • jigujigujigujigu Posts: 3Questions: 2Answers: 0

    Hi Allan,
    Thank You

  • hungtien408hungtien408 Posts: 1Questions: 0Answers: 0
    edited March 2017

    [HttpPost]
    public async Task<IActionResult> OperationData([FromForm] string action, List<string> data) <<--- is this correct ??
    {

    if (action == "remove")
    {
        var customer = await _context.Customers.SingleOrDefaultAsync(m => m.id == data.????);   <<--- how to delete ??
        _context.Customers.Remove(customer);
        await _context.SaveChangesAsync();
    
    }
    return RedirectToAction("Index");
    

    }

    How to assign data to List<string> data, after get data and delete

This discussion has been closed.