【问题标题】:How to send complex types to MVC2 controller via Jquery (Ajax )如何通过 Jquery (Ajax) 将复杂类型发送到 MVC2 控制器
【发布时间】: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


    【解决方案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 时始终使用 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 等区分大小写的语言很重要。

    【讨论】:

    • 感谢您的回复和关于不要硬编码网址的建议。我做了你建议的修复,它仍然不起作用!这来自 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% %5D=&RuleModel%5BRuleId%5D'
    【解决方案2】:

    您可以在控制器操作上编写 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)
    {...}
    

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多