I am using $("#formid").serialize() that returns a string and I need to override one of the values in this string with a different value before making the ajax call.
Should I be using serializeArray()? IF so, how do I get a string after updating the array value?
If this is a conditional override, you can use a hidden field. Based on the condition, store the overriding value, or the actual value in the hidden field. Then on the server side, look at the value of the hidden field instead of the input field that is visible to the user.
Or, you can change the value of the field before you serialize, and then restore it after you call
jQuery.ajax.The first option seems cleaner to me.
UPDATE
If you don’t want to change your view model, then option 2 is the easiest way. There are other ways:
Use a regular expression to replace the value:
serializedString.replace(/FieldName=[^&]+/, "FieldName=" + overridingValue);Split the string and examine each property. This is more time-consuming and verbose, but you can be sure that you’re overriding the right field:
IMO, that seems like a lot of work, so I’d still go with option 2!