有关JSON和序列化问题 [英] Question about JSON and Serialization

查看:147
本文介绍了有关JSON和序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的视图模型,其中包含一个对象Person和技能的名单一个强类型的视图。人是非常简单的。我使用HTML辅助这样的 @ Html.TextBoxFor(M = GT; m.Person.FirstName)。我提交表单,并得到我想要的东西。

I have a strongly typed view with my viewmodel which contains an object Person and an List of Skills. Person is quite straightforward. I use Html Helpers like this @Html.TextBoxFor(m => m.Person.FirstName). I submit my form and get what I want.

问题是我的技能列表。

使用AJAX调用我得到一个JSON结果是技能的数组。

With an AJAX call i get a JSON result which is an array of Skills.

我应该如何发送的两个技能的结合,这个人我HTTPPOST方法?

How should I send the combination of both the skills and the person to my HTTPPOST method?

我看到了两个可能性。


  1. 一个我赞成,但对如何以正确的方式贯彻不知道:不知何故设法让这个JSON结果到我的视图模型(列表<技巧> skilllist ),并使用标准的提交按钮,在我这样的HTTPPOST方法接收它。 (见行内评论)

  1. The one I favour, but have no idea on how to implement in the correct way: Somehow manage to get this JSON result into my viewmodel (List<Skill> skilllist) and use the standard submit button and receive it in my HTTPPOST method like this. (see inline comments)

[HttpPost]
public ActionResult RegisterPersonAndSkills(PersonSkillViewModel model)
{
   // I can acces the Person object and its properties
   string firstname = model.Person.FirstName;

   // It would be awesome if I could access the list which used to be a JSONresult
   string skillname = model.SkillList[0].SkillName

   return null;
}


  • 尝试改变,序列化一切的形式(Person对象部分)转换成JSON结果,将技能的数组JSON结果我到该序列化视图模型,并通过modelbinding方式接收模式。与相同的结果与上面的POST方法。我还是那句话我不知道如何实现这一点,如何应对可能的验证问题。这似乎是一个大量的工作人员的每个属性序列化为JSON,并添加Person对象和技能数组到JSON PersonSkillViewModel。

  • Try transforming, serializing everything that's in the form (Person object part) into a JSON result, insert the array of skills json result I have into that serialized viewmodel and receive the model through the modelbinding way. With the same outcome as the post method above. Again I'm not sure on how to implement this and how to deal with the possible validation issues. It seems a lot of work to serialize every property of Person into JSON, and add the Person object and skill array to a JSON PersonSkillViewModel.

    您将如何解决这个问题呢?

    How would you solve this problem?

    或者是根本不可能在一个参数来获取两个结果?

    Or is it simply impossible to get both results in one parameter?

    推荐答案

    要实现你想要的结果#1最简单的方法,是通过隐藏域客户端加入您的AJAX调用收到的技能阵列,给DOM。你必须使用的命名规则,默认的模型绑定使用的(我没有VS打开,但如果你在填充GET方法你的技能阵列,并添加了每一个Html.HiddenFor&LT;>你应该看到的语法。)

    The simplest way to achieve your desired result #1, is to add the Skills Array you recieved in the AJAX call, to the DOM via hidden fields client side. You'll have to use the naming convention that the default model binder uses (I don't have VS open, but if you populate your Skills Array on the GET method and add a for each with a Html.HiddenFor<> you should see the syntax.)

    所以基本上你需要一些JS来处理JSON请求的客户端,并将其添加到DOM ......这短短的几行使用jQuery,但你没有提到你是如何获得的技能阵列Ajax请求

    So basically you will need some JS to process the JSON request client side and add it to the DOM... this is just a few lines with Jquery, but you didn't mention how your are getting the skills array Ajax request.

    这篇关于有关JSON和序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆