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;
}
}
レースの部分がこのモデルと一致する場所...
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;
}
}