Loading

Mapping complex JSON in MVC Action argument (MVC中Json 复杂对象的使用)

JS source

    <script src="/Script/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Script/jquery.json-2.2.min.js" type="text/javascript"></script>

Mapping Entity


public class someList
{
   public string strOne {get; set;}
   public string strTwo {get; set;}
}
public class Entity
{
    public string EntityMember;
    public List<someList> entityList;
}

Argument


public ActionResult MyControllerAction(Entity objEntity)

JS client

$.ajax({                                                    
        url: "/MyController/MyControllerAction",                                       
        type: "POST",                                           
        data: JSON.stringify(Entity),                          
        dataType: "json",                                       
        contentType: "application/json; charset=utf-8",         
        success: function (data) {                              
            ....                                     
        }                                                       
    });  

My Problems

  • data: JSON.stringify(Entity)//Jquery 默认会转Json obj成键值对,所以必须传送String
  • var Entity = {};//需要跟mapping的类名或action的参数名一致
posted @ 2011-05-13 18:32  today4king  阅读(262)  评论(0编辑  收藏  举报