【发布时间】:2011-02-22 19:42:30
【问题描述】:
我正在向我的控制器发布一个 Jquery Json 序列化对象,但并非所有数据都被传递。其中一个成员是一个复杂类型,也是 Json 序列化的。这是没有通过控制器的那个。
这是我通过 Ajax 帖子传递给我的控制器的类。请注意复杂类型 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; }
}
这是我在 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}]
});
};
这是我的 JQuery Ajax 调用:
$.ajax({
type: "POST",
url: "/LeadRoutingConsole/VendorLeadRouting/PostSourceCode",
data: sourceCodeModel,
datatype: "json",
success: Commit_success,
error: Commit_error,
complete: function (jqXHR) { }
});
这是我的控制器的动作方法:
[HttpPost]
public JsonResult PostSourceCode(SourceCodeModel model)
{
// perform the save op
var viewModel = new SourceCodesViewModel();
viewModel.PostSourceCode(model);
return Json(model);
}
问题:SourceCodeModel 包含正确的值,除了它的复杂成员:RuleModel,它作为具有默认(null 或 0)值的 RoutingRuleModel 返回。
【问题讨论】:
标签: jquery asp.net ajax json asp.net-mvc-2