0

我一直无法停止阅读有关JsonValueProviderASP.NET MVC 的伟大和美妙的内容,并且我非常拼命地尝试让它工作,但是这个结果变得非常令人沮丧。几天来,我一直在抨击我的脸,这只是到了我不知道出了什么问题的地步。

使用以下控制器动作..

[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;
    }
}
4

1 回答 1

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 发送。

于 2013-09-26T20:53:14.870 回答