c# - MVC 4 中使用 Json 使用 ajax 的困难

标签 c# asp.net-mvc jquery

我试图将一个变量传递给我的 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中的所有内容都为空

我的模型:

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

}

最佳答案

尝试为 testea 变量指定名称,以确保操作方法将其分配给相同的命名参数,如下所示:

$.ajax({
    url: url,
    type: "post",
    data: { testea: testea },
    dataType: 'json',
    success: function (data) {
        alert(data.success);
    },
   error: function () {
       alert("error");
    }
});

关于c# - MVC 4 中使用 Json 使用 ajax 的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14486380/

相关文章:

c# - Microsoft 标准计算器中使用的变量类型

c# - 对于 Windows 服务,等待旋转或计时器哪个更好?

jquery - $.each(someData.split (',' ) => 返回连接字符串,而不是项目数组

c# - HiddenFor 应该尊重 DisplayFormat 吗?

javascript - 添加下拉值的新输入字段

c# - WPF:如何在 Key* 事件中检测 Key 重复?

c# - 通过Web.Config参数动态生成 View

jquery - 使用 Rails 和 jquery 进行动态选择

javascript - 这是 jquery 的跨域问题吗?

C# - 使用 session 变量将用户 ID 从选定的 GridView 列传递到另一个页面