jquery - MVC: How to bind array in hidden form field value to model collection property -


i have saved collection of ints in hidden field using jquery (this collection of pkids's use store data across several pages).

in viewmodel have property:

public ienumerable<int> subgrouppkids { get; set; } 

in view have:

<input type="hidden" id="checkedsubgroups" value="[]" /> 

the save button on form uses jquery serialize , post info controller:

var $form = $(self.options.pushformid);             if (!$form.valid())                 return;             var data = $form.serialize();              $.ajax({                 type: $form.prop('method'),                 url: $form.prop('action'),                 data: data,                 success: function (data) {                     showpushdialog(data);                 },             }); 

the post method of collection has signature

[httppost]     public async task<actionresult> pushpurchaser([bind]pushpurchaserviewmodel model) 

so stands need save collection of ints have on form collection of ints have in model can bound model can use in controller post method. i'm not quite sure how though.

if using razor, add hidden fields in form:

for (int = 0; < model.subgrouppkids.count; i++ )     {   @html.hidden("subgrouppkids[" + + "]", model.subgrouppkids[i]) } 

and change public ienumerable<int> subgrouppkids { get; set; } to

public list<int> subgrouppkids { get; set; } 

you can change value using jquery,

 $(document).ready(function () {     $('input[name="subgrouppkids[0]"]').val(5);  }); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -