I am posting a Jquery Json serialized object to my controller but not all of the data is getting passed. One of the members is a complex type that is also Json serialized. This is the one that isn't getting through to the controller.
Here is the class I'm passing to my controller via Ajax post. Note the complex type, RoutingRuleModel.
SourceCodeModel.cs:
[Serializable]
public class SourceCodeModel
{
public string SourceCode { get; set; }
public bool IsActive { get; set; }
public string LastChangedBy { get; set; }
public string LocationCode { get; set; }
public string Vendor { get; set; }
public RoutingRuleModel RuleModel { get; set; }
}
RoutingRuleModel.cs:
[Serializable]
public class RoutingRuleModel
{
public string AdaStuInfoSysId { get; set; }
public string AdaInitials{ get; set; }
public string LocationCode { get; set; }
public string Vendor { get; set; }
public string RuleName { get; set; }
public string RuleStatus { get; set; }
public int RuleId { get; set; }
}
Here is how I am building the model in JavaScript:
getSourceCodeModel = function (sourceCode, isActive, lastChangedBy, locationCode, ruleModel) {
// Retrieves a SourceCodeModel object that can be JSON-serialized
return ({
SourceCode: sourceCode,
IsActive: isActive,
LastChangedBy: lastChangedBy,
LocationCode: locationCode,
Vendor: ruleModel.Vendor,
RuleModel: [{ AdaStuInfoSysId: ruleModel.AdaStuInfoSysId, AdaInitials: ruleModel.AdaInitials, LocationCode: ruleModel.locationCode,
Vendor: ruleModel.Vendor, RuleName: ruleModel.RuleName, RuleStatus: ruleModel.RuleStatus, RuleId: ruleModel.RuleId}]
});
};
Here is my JQuery Ajax call:
$.ajax({
type: "POST",
url: "/LeadRoutingConsole/VendorLeadRouting/PostSourceCode",
data: sourceCodeModel,
datatype: "json",
success: Commit_success,
error: Commit_error,
complete: function (jqXHR) { }
});
Here is my controller's action method:
[HttpPost]
public JsonResult PostSourceCode(SourceCodeModel model)
{
// perform the save op
var viewModel = new SourceCodesViewModel();
viewModel.PostSourceCode(model);
return Json(model);
}
Problem: SourceCodeModel contains correct values EXCEPT for it's complex member: RuleModel, which comes back as a RoutingRuleModel with default (null or 0's) values.