c# - How to submit form containing multi-page table with input elements -


i'm using bootstrap on table , datatables.net integrate searching , paging well. problem current page of table retained on model after clicking submit button.

without integrating searching , paging via datatables.net, there no errors when datatables.net plugin used.

model:

public class sampleviewmodel {     public list<collectionviewmodel> collection { get; set; } }  public class collectionviewmodel {     public string name { get; set; }     public int? value { get; set; } } 

controller:

public actionresult sample() {     sampleviewmodel model = new sampleviewmodel();     model.collection = new list<collectionviewmodel>();     model.collection.add(new collectionviewmodel { name = "test1" });     model.collection.add(new collectionviewmodel { name = "test2" });     model.collection.add(new collectionviewmodel { name = "test3" });     model.collection.add(new collectionviewmodel { name = "test4" });     model.collection.add(new collectionviewmodel { name = "test5" });     model.collection.add(new collectionviewmodel { name = "test6" });     model.collection.add(new collectionviewmodel { name = "test7" });     model.collection.add(new collectionviewmodel { name = "test8" });     model.collection.add(new collectionviewmodel { name = "test9" });     model.collection.add(new collectionviewmodel { name = "test10" });     model.collection.add(new collectionviewmodel { name = "test11" });     model.collection.add(new collectionviewmodel { name = "test12" });     model.collection.add(new collectionviewmodel { name = "test13" });     model.collection.add(new collectionviewmodel { name = "test14" });     model.collection.add(new collectionviewmodel { name = "test15" });      return view(model); }  [httppost] public actionresult sample(sampleviewmodel model) {     var ctr = model.collection.count(x => x.value != null);      return view(model); } 

view:

@model myapp.models.sampleviewmodel  @using (html.beginform()) { <div class="datatable_wrapper">     <div class="pull-right">         <button type="submit" name="submitbutton" class="btn btn-primary btn-sm">             <i class="fa fa-floppy-o fa-1x"></i>             submit         </button>     </div><br /><hr />     <table class="table table-striped table-bordered table-hover">         <thead>             <tr>                 <th>name</th>                 <th>value</th>             </tr>         </thead>         <tbody>              @for (int = 0; < model.collection.count(); ++i)              {                  @html.hiddenfor(model => model.collection[i].name)                 <tr>                     <td>@html.displayfor(model => model.collection[i].name)</td>                     <td>                         @html.textboxfor(model => model.collection[i].value, new { @class = "form-control" })                     </td>                 </tr>              }         </tbody>     </table> </div> } 

before submit: before submit

after submit button clicked: after submit button clicked

you can see on above picture instead of 15 records, 10 records stored on model.

cause

when using datatables plug-in pagination current page <tr> elements (10 in example) exist in dom performance. therefore when submit form, current page form controls values being submitted.

solution 1: submit form directly

the trick turn form elements pages other current <input type="hidden"> before submitting form.

var table = $('#example').datatable();  // handle form submission event $('#frm-example').on('submit', function(e){    var form = this;     // encode set of form elements pages array of names , values    var params = table.$('input,select,textarea').serializearray();     // iterate on form elements    $.each(params, function(){       // if element doesn't exist in dom       if(!$.contains(document, form[this.name])){          // create hidden element          $(form).append(             $('<input>')                .attr('type', 'hidden')                .attr('name', this.name)                .val(this.value)          );       }    }); }); 

see this example code , demonstration.

solution 2: submit form via ajax

another solution submit form via ajax.

var table = $('#example').datatable();  // handle form submission event $('#frm-example').on('submit', function(e){    // prevent actual form submission    e.preventdefault();     // serialize form data    var data = table.$('input,select,textarea').serialize();     // submit form data via ajax    $.ajax({       url: '/echo/jsonp/',       data: data,       success: function(data){          console.log('server response', data);       }    }); }); 

see this example code , demonstration.

notes

please note both solutions work in client-side processing mode only.

links

see jquery datatables: how submit pages form data more details.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -