ASP.NET MVC3JSON ModelBindersな
-
26-09-2019 - |
質問
ようにしているのJSON ModelBindersにMVC3スコット-ガスリーが語る 彼のブログ.
私の例では、彼はます。いモデルの3つの価値を今後、サーバーにコピーします。
このモデルは以下のようなものです:
public class CommentViewModel
{
public string Product { get; set; }
public string Text { get; set; }
public string Author { get; set; }
}
JavaScriptのようになります。
$("#addComment").click(function () {
var comment = {
Product: $("#productName").html(),
Text: $("#comment").val(),
Author: $("#author").val()
};
alert("value=" + JSON.stringify(comment));
$.ajax({
url: "/Home/Add",
type: "POST",
data: JSON.stringify(comment),
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
}
});
});
のコントローラのアクションは以下のようなものです:
[HttpPost]
public ActionResult Add(CommentViewModel comment)
{
// ...
}
りなさんの中にJavaScript)で与えてくれるものが好きです:
value={"Product":"Classic","Text":"the comment","Author":"me"}
期待したいの特性をモデルに入力され、サーバがすべての特性をnullになります。を使用していASP.NET MVC3プレビュー1.
解決
っていたのか、ASP.NET MVC3 1プレビューではないの登録 JsonValueProviderFactory
automagicallyとして期待される。
を入れて次の抜粋です。asaxを手動で登録すべきである固定解決問題:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())
他のヒント
くいただけ変更する必要がある以下の路線
$.ajax({
url: "/Home/Add",
type: "POST",
data: comment,
datatype: "json",
success: function (data) {
alert(data);
}
});
知っさstringifyのcontenttype
所属していません StackOverflow