I'm trying to tack on some additional data a POST request sent to my server. Originally, I was sending just several forms worth of information:
$.ajax({
url: 'SaveAllDetails',
type: 'POST',
data: $('form').serialize(),
dataType: 'json'
});
and the MVC Controller method:
[HttpPost]
public ActionResult SaveAllDetails([Bind(Prefix = "order")]ExistingOrderDetailsModel existingOrderDetailsModel,
[Bind(Prefix = "task")]ExistingTaskDetailsModel existingTaskDetailsModel, [Bind(Prefix = "device")]DeviceDetailsModel deviceDetailsModel)
{
....
}
This works great. MVC's model binder is able to correctly de-serialize the URL-encoded string.
Now, requirements have changed. I need to send an additional array of data along with my three forms. This array of data is not held within a form and does not have a binding prefix. I need to do this all in the same Controller method because all validation needs to be performed inside of a single transaction.
So, I've now got:
var subcomponentsGridRows = JSON.stringify(subcomponentsDetailsView.getAllGridData());
var existingOrderDetailsFormData = $('form#existingOrderDetailsForm').serialize();
var existingTaskDetailsFormData = $('form#existingTaskDetailsForm').serialize();
var deviceDetailsFormData = $('form#existingDeviceDetailsForm').serialize()
$.ajax({
url: 'SaveAllDetails',
type: 'POST',
data: {
existingOrderDetailsModel: existingOrderDetailsFormData,
existingTaskDetailsModel: existingTaskDetailsFormData,
deviceDetailsModel: deviceDetailsFormData,
subcomponentsGridRows: subcomponentsGridRows
},
dataType: 'json'
});
This doesn't work for at least one reason. Each form is represented as a URL-encoded string. subcomponentsGridRows is a JSON structure. The MVC model binder isn't capable of deciphering both types of information in one go as far as I can tell.
What's a good way to go about tackling this problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…