JsonValueProvider似乎不适用于ASP.NET MVC 4

时间:2013-09-26 20:50:13

标签: jquery asp.net-mvc json

我无法停止阅读有关ASP.NET MVC中的JsonValueProvider多么伟大和精彩的内容,我正在非常拼命地让它成功,但这个结果让人感到非常沮丧。我几天和几天一直在抨击我,这只是我不知道出了什么问题。

使用以下控制器操作..

[HttpPost]
public JsonResult Character(ViewModels.CreateCharacterViewModel model) {

    // if the character model is valid, we can go ahead and start looking at creating it.
    if (ModelState.IsValid) {
    }
    return null;
}

有了这个AJAX帖子......

$.ajax({
    url: '/member/create/character',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: {
        model: JSON.stringify(viewModel)
    }
});

我在发帖时收到错误Invalid JSON Primitive,这是我的数据......

{
  "Name": "Test Name",
  "Age": "21",
  "Gender": "None",
  "Points": [

  ],
  "Race": {
    "Id": "races/4",
    "Name": "Test Race",
    "Url": "description-url.html",
    "Description": null,
    "Genders": [
      "None"
    ],
    "Lifespan": {
      "Minimum": 0,
      "Maximum": 10000000
    },
    "Points": null
  }
}

它应对应于此C#视图模型

/// <summary>
/// Defines a view model for creating a new character.
/// </summary>
public class CreateCharacterViewModel {

    /// <summary>
    /// The character's name as it will appear on the character sheet, and on the roster.
    /// </summary>
    [Required]
    [DataType(DataType.Text)]
    [RegularExpression(Text.RegularExpressions.Name, ErrorMessage = Text.ErrorMessages.Name)]
    [Display(Name = "Character Name")]
    [Rules("The name of the character. Names must be between 3 and 64 characters in length, may contain any alphanumeric character and the symbols -', and spaces only.")]
    public string Name {
        get;
        set;
    }

    /// <summary>
    /// The email address of the character.
    /// </summary>
    [Required]
    [DataType(DataType.EmailAddress)]
    [RegularExpression(Text.RegularExpressions.Email, ErrorMessage = Text.ErrorMessages.Email)]
    [Display(Name = "Email Address")]
    [Rules("The email address for the character. This does not have to be unique. You can't use: <em>[ ] | ; , $ \\ < > \" or any spaces.</em>")]
    public string Email {
        get;
        set;
    }

    /// <summary>
    /// The character's age in standard years.
    /// </summary>
    [Required]
    [Integer]
    [Display(Name = "Character Age")]
    [Rules("The character's current age, given in years. This field is required, even if your character is not aware of their own age.")]
    public int Age {
        get;
        set;
    }

    /// <summary>
    /// The character's selected race
    /// </summary>
    [Required]
    [Display(Name = "Race")]
    [Rules("The character's race. You may select from our pre-defined templates, or select 'custom' if the race you wish to make does not exist.")]
    public Models.Races.Race Race {
        get;
        set;
    }

    /// <summary>
    /// The character's selected gender.
    /// </summary>
    [Required]
    [Display(Name = "Gender")]
    [Rules("The character's gender. This must be selected. Not all possible options are available to all races.")]
    public string Gender {
        get;
        set;
    }
}

Races部分与此模型匹配......

public class Race : IHasIdentity, IHasName, IMayTemplate {
    /// <summary>
    /// The unique identity of the race.
    /// </summary>
    public string Id {
        get;
        set;
    }

    /// <summary>
    /// The unique name of the race.
    /// </summary>
    public string Name {
        get;
        set;
    }

    /// <summary>
    /// The URL that points to the race's detail page.
    /// </summary>
    public string Url {
        get;
        set;
    }

    /// <summary>
    /// The description of the race.
    /// </summary>
    public string Description {
        get;
        set;
    }

    /// <summary>
    /// The genders available to the race.
    /// </summary>
    public List<string> Genders {
        get;
        set;
    }

    /// <summary>
    /// The race's expected lifespan
    /// </summary>
    public Lifespan Lifespan {
        get;
        set;
    }

    /// <summary>
    /// The various customization points that the race has to spend.
    /// </summary>
    public List<Points> Points {
        get;
        set;
    }
}

1 个答案:

答案 0 :(得分:3)

试试这样:

$.ajax({
    url: '/member/create/character',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(viewModel)
});

或者如果您想明确指定动作参数的名称:

$.ajax({
    url: '/member/create/character',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ model: viewModel })
});

注意在我的示例中如何将整个data属性作为JSON.stringify发送,而在您的示例中,您只将model属性作为JSON发送。