ASP.NET MVC Jquery post dynamic form -


i have html form has 2 divs, 1 div fixed area means inputs not dynamic, , second div dynamic means have button can clone div many times want.

this jquery clode cloning new div:

 $('#btn-addsection').click(function (e) {                 var me = $(this);                 e.preventdefault();                  var lastrepeatinggroup = $('.repeating-section').last();                 var cloned = lastrepeatinggroup.clone(false)                 cloned.insertafter(lastrepeatinggroup);                  var attrs = ['for', 'id', 'name'];                  var tags = section.find('input, label'),                     idx = section.index();                  tags.each(function () {                     var $this = $(this);                     if ($this.is('input')) {                         $this.val('');                     }                     $.each(attrs, function (i, attr) {                         var attr_val = $this.attr(attr);                         if (attr_val) {                             $this.attr(attr, attr_val.replace(/_\d+$/, '_' + (idx)))                         }                     })                 })               }); 

the above code clone div , add underscore "_" plus next index field can identify field second or third, etc. div cloned.

when post server use:

 var formdata = $('#form-newquote :input').serializearray(); 

this save method on server:

public jsonresult savequote(quotemodel model)         {              var response = this.jsonresponse;              response = new jsonresponse             {                 data = null,                 message = string.empty,                 num = 0,                 success = true,                 code = null             };              return json(response, jsonrequestbehavior.allowget);         } 

this quotemodel:

enter image description here

the red mark many many cloned items. problem how send jquery server dynamic fields on server can map them quoteselectionmode object.

any clue?

you should use $('form').serialize() instead. have never used serializearray. looking @ documentation, creates array of objects name , value properties don't think modelbinder able map quotemodel.

changing serialize() still won't bind sections property because of how you're handling name attribute.

it's crucial set name attribute whenever you're dealing properties collcetions of objects. in case, inputs should like:

<input type="text" name="sections[0].propertname" /> <input type="text" name="sections[0].propertname2" /> <select name="sections[0].propertname3" /> 

when cloned, should produce this:

<input type="text" name="sections[1].propertname" /> <input type="text" name="sections[1].propertname2" /> <select name="sections[1].propertname3" /> 

besides this, make sure in ajax, you've given same parameter name controller:

$.ajax({     url: "/controllername/savequote",     data: { model: formdata },     ........... 

also, consider using knockout kind of recurring templates. cloning using jquery error-prone , you'll have write lot of junk code. in ko, observalblearray.push(new viewmodel()) , you're set.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -