0

JSON数组如下

[
  {
    "id":1,
    "Name":"hvhv",
    "Marks":11,
    "Grade":"hvh"
  },
  {
    "id":2,
    "Name":"f",
    "Marks":12,
    "Grade":"yf"
  },
  {
    "id":3
    "Name":"ds",
    "Marks":13,
    "Grade":"jhj"
  }
]

我希望我的控制器方法中这个数组的所有 ID 作为模型/字符串数组

我有一个名为 registerModel 的 RegisterModel 对象,我想将所有 id 发布到 registerModel.UserId 和 Name 到 registerModel.Name

4

2 回答 2

1

如果您在控制器操作中收到此类 JSON,您可以尝试按如下方式使用它。创建类似模型:

public class MyModel
{
 public int id {get; set;}
 public string Name {get; set;}
 public int Marks {get; set;}
 public sytring Grade {get; set;}
}

然后在您的 Controller 操作中:

public ActionResult MyAction (List<MyModel> models)
{
  foreach(var item in models)
  {
    var result = new registerModel 
      {
        name = item.Name,
        userID = id
       }
      //Do somthing with registerModel
   }
}
于 2013-10-03T10:00:18.093 回答
1

HttpContext.Request您可以从对象接收 json到您的模型列表,例如,

        public ActionResult MyAction()
        {
            var resolveRequest = HttpContext.Request;
            List<RegisterModel> registerModelList = new List<RegisterModel>();
            resolveRequest.InputStream.Seek(0, SeekOrigin.Begin);
            string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd();
            if (jsonString != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                registerModelList = (List<RegisterModel>)serializer.Deserialize(jsonString, typeof(List<RegisterModel>));
            }
         }
于 2013-10-03T10:02:45.187 回答