zoukankan      html  css  js  c++  java
  • Mvc ModelBinder 一对多自定义数据格式 post提交

    Mvc ModelBinder
    View Code

    webapi ModelBinder

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Http.Controllers;
     6 using System.Web.Http.ModelBinding;
     7 using Newtonsoft.Json;
     8 using Newtonsoft.Json.Linq;
     9 using System.Net;
    10 namespace Ecomm.Mvc.Web.Areas.ApiServices.Models
    11 {
    12     /// <summary> 
    13     /// Json数据绑定类 
    14     /// </summary> 
    15     /// <typeparam name="T"></typeparam> 
    16     public class ApiJsonBinder<T> : System.Web.Http.ModelBinding.IModelBinder
    17     {
    18         public bool BindModel(HttpActionContext controllerContext, ModelBindingContext bindingContext)
    19         {
    20             //从请求中获取提交的参数数据 
    21             //var json1 = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    22             //string json = json1.RawValue as string;
    23             
    24             var json = controllerContext.Request.Content.ReadAsStringAsync().Result;
    25             json=HttpUtility.UrlDecode(json);
    26             //var json = controllerContext.Request.Headers.From[bindingContext.ModelName] as string;
    27             //提交参数是对象 
    28             if (json.StartsWith("{") && json.EndsWith("}"))
    29             {
    30                 JObject jsonBody = JObject.Parse(json);
    31                 JsonSerializer js = new JsonSerializer();
    32                 object obj = js.Deserialize(jsonBody.CreateReader(), typeof(T));
    33                 bindingContext.Model = obj;
    34                 return true;
    35             }
    36             //提交参数是数组 
    37             if (json.StartsWith("[") && json.EndsWith("]"))
    38             {
    39                 IList<T> list = new List<T>();
    40                 JArray jsonRsp = JArray.Parse(json);
    41                 if (jsonRsp != null)
    42                 {
    43                     for (int i = 0; i < jsonRsp.Count; i++)
    44                     {
    45                         JsonSerializer js = new JsonSerializer();
    46                         object obj = js.Deserialize(jsonRsp[i].CreateReader(), typeof(T));
    47                         list.Add((T)obj);
    48                     }
    49                 }
    50                 //return list;
    51                 bindingContext.Model = list;
    52                 return true;
    53             }
    54              return false;
    55         }
    56     }
    57 }
    View Code
     public HttpResponseMessage PostProduct([ModelBinder(typeof(ApiJsonBinder<Ecomm.Mvc.Web.Models.SubmitJsonModels.Product>))]
                 Product product)
            {
            }    
    //前端使用 ajax 提交
    Content-Type: application/json
    data:{"一对多json数据"}

    如果业务能分解的话 最好还是使用默认 formdata 提交单行数据

    但是有时没办法需要提交 一对多数据时 mvc默认绑定机制就不能进行处理了。这里需要我们自己写代码来实现了

    前端使用 js 手动吧json数据 组织好后 提交到后台 自定义 IModelBinder 进行 反序列化处理(json.net库) 

  • 相关阅读:
    单例模型
    数据库7 索引
    数据库6.高级
    数据库5 不想改
    绑定方法与非绑定方法 反射 内置方法
    组合 封装 多态
    面向对象之继承
    面向过程编程
    logging hashlib 模块
    pickle json xml shelve configparser模块
  • 原文地址:https://www.cnblogs.com/wilsonjw/p/3620703.html
Copyright © 2011-2022 走看看