locked
Datatables with Checkbox for Model & Controller RRS feed

  • Question

  • User-1013792694 posted

    I'm very exciting to use datatable with checkbox from this example for bulk approval.
    What I'm facing, right now, are

    1. How to prepare Model that suitable when all the checked some IDs and send to Controller. I use 'serialize' to send data from view to controller.
    2. In this case, my project use stored procedure. Do I have to send all of those bulk IDs by using asynchronous process (await & async).

    this my view code

    var dataToPost = $('#frm_Approval').serialize(); alert(dataToPost); $.post("Payment_Approval", dataToPost) .done(function (data) { })

    Please advise me.

    Wednesday, November 7, 2018 3:11 AM

Answers

  • User1520731567 posted

    Hi daleman,

    I think you could use model bind to pass all the checked some IDs to Controller.

    For example:

    Model:

     public class CheckboxViewModel
            {
                ...
                public IList<SelectListItem> Carrier { get; set; }//define data source of listbox 
                public List<string> SelectedCarrierId { get; set; }
    
            }

    View:

    @model xxx.CheckboxViewModel
    
    ...
    
    @Html.ListBoxFor(model => model.SelectedCarrierId, Model.Carrier, new { @class = "col-md-2" })
    ...
    <button type="submit" class="btn btn-primary">Send</button>
    ...

    Here is the demo,you could refer to:

    https://forums.asp.net/t/2146573.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 9:14 AM

All replies

  • User1120430333 posted

    You should use Ajax and use a service.

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    The service can be accessible via Ajax that calls a post method on the MVC controller action that in turn call a service  for data persistence like the service using a stored procedure.

    https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx

    Wednesday, November 7, 2018 5:43 AM
  • User1520731567 posted

    Hi daleman,

    I think you could use model bind to pass all the checked some IDs to Controller.

    For example:

    Model:

     public class CheckboxViewModel
            {
                ...
                public IList<SelectListItem> Carrier { get; set; }//define data source of listbox 
                public List<string> SelectedCarrierId { get; set; }
    
            }

    View:

    @model xxx.CheckboxViewModel
    
    ...
    
    @Html.ListBoxFor(model => model.SelectedCarrierId, Model.Carrier, new { @class = "col-md-2" })
    ...
    <button type="submit" class="btn btn-primary">Send</button>
    ...

    Here is the demo,you could refer to:

    https://forums.asp.net/t/2146573.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 9:14 AM
  • User-474980206 posted

    serialize() will only included checkboxes that are enabled and checked. but if you used checkbox() instead of html <input>, the checkbox helper creates a hidden field for each <input> (to post false). so to keep the list small, just use <input type=checkbox>

    Thursday, November 8, 2018 4:09 PM