如何通过Jquery(Ajax)将复杂类型发送到MVC2控制器

时间:2011-02-22 19:42:31

标签: jquery asp.net ajax json asp.net-mvc-2

我将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包含正确的值EXCEPT,因为它的复杂成员:RuleModel,它以具有默认值(null或0)的RoutingRuleModel的形式返回。

2 个答案:

答案 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中那样对网址进行硬编码。在处理网址时始终使用网址助手,否则在部署应用时可能会出现意外情况:

$.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)非常重要。

答案 1 :(得分: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)
{...}