2

JsonResultに変数を渡そうとしていますが、彼女はnulになるので、JsonResultとjQueryからのコードを次に示します。

    [HttpPost]
    public JsonResult MostraTela(Teste testea)
    {

        return Json(new { success = true });
    }

と :

   var testea = JSON.stringify(dado);
                    $.ajax({
                        url: '/Home/MostraTela',
                        type: 'POST',
                        dataType: 'json',
                        contentType: "application/json; charset=utf-8",
                        data: {testea: testea },
                        success: function (data) {
                            alert(data.success);
                        },
                        error: function () {
                            alert("error");
                        },

                    });

Agora eu fui tentar passar uma model e esta esta recebendo nulo novamente alguma ideia do que pode ser?データをステップアップします:{testea:testea}、エラー。データをステップアップすると:testea、JsonResultですべてがnullになります

私のモデル:

public class Teste
{
    public int idteste { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string birthday { get; set; }
    public string salary { get; set; }

}
4

3 に答える 3

2

次のように、testea変数に名前を付けて、actionメソッドが同じ名前のパラメーターに割り当てていることを確認してください。

$.ajax({
    url: url,
    type: "post",
    data: { testea: testea },
    dataType: 'json',
    success: function (data) {
        alert(data.success);
    },
   error: function () {
       alert("error");
    }
});
于 2013-01-23T18:18:54.840 に答える
0

ターゲットメソッドのシグネチャは文字列であるため、123を文字列として渡してみてください。

var testea = JSON.stringify("123");
于 2013-01-23T18:14:16.530 に答える
0

JSONを渡す場合は、をに設定content-typeapplication/jsonます。

$.ajax({
    type: 'POST',
    url: '@Url.Action("MostraTela", "Home")',
    data: { Id: 1, Name: 'Im a complex object' },
    success: function (data) {
        alert(data.success);
    },
    dataType: 'json',
    contentType: 'application/json, charset=utf-8',
});

コントローラ:

[HttpPost]
public JsonResult MostraTela(MyModel model, FormCollection collection)
{
    return Json(new { success = true });
}

クラス:

public class MyModel
{
    public string Name { get; set; }
    public int Id { get; set; }
}
于 2013-01-23T18:26:03.857 に答える