Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send complex types to MVC2 controller via Jquery (Ajax )
    primarykey
    data
    text
    <p>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.</p> <p>Here is the class I'm passing to my controller via Ajax post. Note the complex type, RoutingRuleModel. </p> <p>SourceCodeModel.cs:</p> <pre><code>[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; } } </code></pre> <p>RoutingRuleModel.cs:</p> <pre><code>[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; } } </code></pre> <p>Here is how I am building the model in JavaScript:</p> <pre><code>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}] }); }; </code></pre> <p>Here is my JQuery Ajax call:</p> <pre><code>$.ajax({ type: "POST", url: "/LeadRoutingConsole/VendorLeadRouting/PostSourceCode", data: sourceCodeModel, datatype: "json", success: Commit_success, error: Commit_error, complete: function (jqXHR) { } }); </code></pre> <p>Here is my controller's action method: </p> <pre><code>[HttpPost] public JsonResult PostSourceCode(SourceCodeModel model) { // perform the save op var viewModel = new SourceCodesViewModel(); viewModel.PostSourceCode(model); return Json(model); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload