Serializing and submitting a form using an AJAX call in JQuery -
i'm having trouble serializing form within mvc.net application using ajax call defined follows:
@using (html.beginform("specialnotice", "displays", formmethod.post, new { id = "form" }))
the form defined follows , posted following controller:
public actionresult specialnotice(string dtype, bool? immediate,specialnoticemodel mod)
when use $(formid).submit()
works fine, form passed controller , specialnoticemodel populated correct information has been defined in form.
however want ajax request instead, because not want form refresh upon submission. ajax call using is:
var formdata = $('#form').serialize(); $.post(window.baseurl + 'displays/specialnotice', { dtype: $(displayid).val(), immediate: false, data: formdata }, function () { var device = $('#devicetype').val(); var postnow = '@model.postnow.tostring()'; window.location.href = window.baseurl + 'displays/specialnotice?immediate=' + postnow + '&dtype=' + device; });
the problem getting form not being serialized in way default submit doing it, the value of mod
when passed controller blank default values. have tried renaming data in ajax call mod didn't work either.
is there way serialize form manually in way $(formid).submit()
seems do?
answer - problem after serializing form wrapping in json object causing problems. instead did , worked fine:
$.post(window.baseurl + 'displays/specialnotice', formdata, function () { var device = $('#devicetype').val(); var postnow = '@model.postnow.tostring()'; window.location.href = window.baseurl + 'displays/specialnotice?immediate=' + postnow + '&dtype=' + device; });
Comments
Post a Comment