2011-02-22 56 views
1

我正在发布一个Jquery Json序列化对象到我的控制器,但并非所有的数据都通过了。其中一个成员是Json序列化的复杂类型。这是没有通过控制器。如何通过JQuery发送复杂类型到MVC2控制器(Ajax)

这是我通过Ajax post传递给我的控制器的类。请注意复杂类型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,这回来的RoutingRuleModel用默认值(null或0的)值。

回答

1

您正在尝试发送集合作为RuleModel而这不是集合属性。因此,尝试像这样(去掉方括号[]围绕RuleModel属性定义):

getSourceCodeModel = function (sourceCode, isActive, lastChangedBy, locationCode, ruleModel) { 
    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 
     } 
    }); 
}; 

,你在你的JavaScript确实也从来没有硬编码的URL。使用URL处理时,必须使用网址助手或当你部署应用程序,你可能会得到不好的结果:

$.ajax({ 
    type: 'POST', 
    url: '<%= Url.Action("PostSourceCode", "VendorLeadRouting") %>', 
    data: sourceCodeModel, 
    dataType: 'json', 
    success: Commit_success, 
    error: Commit_error, 
    complete: function (jqXHR) { } 
}); 

还要注意,参数被称为的dataType: 'json'代替datatype: 'json'它是区分大小写的语言很重要如javascript。

+0

谢谢您的答复,并在没有硬编码的URL的建议。我提出了修复建议,但仍然无效!这是来自FireBird:'SourceCode = 333www&IsActive = 1&LastChangedBy =&LocationCode = AICA&Vendor = STUSCOUT&RuleModel%5BAdaStuInfoSysId%5D =&RuleModel%5BAdaInitials%5D =&RuleModel%5BLocationCode%5D = AICA&RuleModel%5BVendor%5D = STUSCOUT&RuleModel%5BRuleName%5D =&RuleModel%5BRuleStatus %5D =&RuleModel%5BRuleId%5D” – cindy 2011-02-22 22:37:22

0

您可以在控制器操作上编写CustomFilterAttribute,并在CustomFilterAttribute类中反序列化json对象。喜欢的东西...

public class JsonFilter : ActionFilterAttribute 
{ 
    public string Param { get; set; }   
    public Type JsonType { get; set; } 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SourceCodeModel)); 
     filterContext.HttpContext.Request.InputStream.Position = 0; 
     SourceCodeModel result = serializer.ReadObject(filterContext.HttpContext.Request.InputStream) 
       as SourceCodeModel; 
     filterContext.ActionParameters[Param] = result; 
    } 
} 

和控制器动作......

[HttpPost] 
[JsonFilter(Param = "sourceCodeModel", JsonType = typeof(SourceCodeMode))] 
public JsonResult PostSourceCode(SourceCodeModel model) 
{...}