JQuery使用Json来调用Controller / Action

时间:2009-04-13 19:40:17

标签: jquery asp.net-mvc

我有一个搜索页面,页面顶部是搜索条件,带有搜索按钮。屏幕底部是按下搜索按钮时的结果。在这种情况下,我有6个不同的搜索条件,用户可以输入。我想将所有标准捆绑到一个类中,因此我的Controller操作可以将Json对象作为类读取。使用FireBug我能够看到我的Json正确构建。使用调试器我知道我的Controller / Action被解雇了。但是,当我在Controller / Action中使用调试器查看类对象时,所有属性都为null或零。

这是我的控制器.... 的[AcceptVerbs(HttpVerbs.Post)]         public ActionResult GetStudentByCritera(StudentSearchCriteraCV critera)         {             //获取数据             ViewData [“MainData”] = studentBLLHdl.StudentFind(critera);             return View();         }

这是调用控制器的JavaScript / JQuery ......              函数LoadResultTable()         {             //构建要发送的Json对象             var dataToSend = BuildJson()

        $.ajax({
            url: "GetStudentByCritera",
            type: 'POST',
            data: dataToSend,
            dataType: 'json',
            contentType: "application/json; charset=utf-8", 
            beforeSend: ClientSideValidate,
            success: function(result) 
            {
                alert(result.Result);
                $('#SearchResult').html(result.Result).show();
                // UnBlock UI
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) 
            {
                // UnBlock UI

                // not sure how to handel error
                alert("error happen when posting to 'GetStudentByCritera'")

                // typically only one of textStatus or errorThrown 
                // will have info
                this; // the options for this ajax request
            }


        });
    }

    function BuildJson()
    {
        // building Json            

        var dataForClass = { "StudentSearchCriteraCV" : [{
            "StudLname": $("input[name='StudentSearchCriteraCV.StudLname']").val(),
            "StudFname": $("input[name='StudentSearchCriteraCV.StudFname']").val(),
            "Ssn": $("input[name='StudentSearchCriteraCV.Ssn']").val(),
            "StudId": $("input[name='StudentSearchCriteraCV.StudId']").val(),
            "Sex": $("input[name='StudentSearchCriteraCV.Sex']").val(),
            "Race": $("input[name='StudentSearchCriteraCV.Race']").val()
            }]};

        return $.toJSON(dataForClass );
     }

    function ClientSideValidate() 
    { 
        // Block the UI
        alert( "In the ClientSideValidate" );

        // if error UNBlock UI
        // return true if client side data is good.

        return true; 
    } 


</script>

3 个答案:

答案 0 :(得分:3)

您的ajax调用只是一个异步HTTP帖子,因此data参数只能是键值对,而不是JSON对象。如果您将数据展平为Class,它将起作用。

答案 1 :(得分:2)

您需要创建一个继承自ActionFilterAttribute的动作过滤器。请查看http://forums.asp.net/t/1237429.aspxhttp://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=863,了解有关如何构建此内容的更多详细信息。

答案 2 :(得分:0)

我认为您的JSON值必须在引号中,除非它是一个数字。

"StudFname": '"' + $("input[name='StudentSearchCriteraCV.StudFname']").val() + '"',

我没有使用.toJSON(),但我相信它会将你的JSON对象变成一个String。由于您正在创建对象以便立即将其转换为字符串,为什么不将其创建为字符串以开始?这就是我遇到这个问题时所做的事情(调试器显示值为零。)

var dataForClass = '{ "StudentSearchCriteraCV" : [{ ' +
        '"StudLname": "' + $("input[name='StudentSearchCriteraCV.StudLname']").val() + '", ' +
        '"StudFname": "' + $("input[name='StudentSearchCriteraCV.StudFname']").val() + '", ' +
        '"Ssn": "' + $("input[name='StudentSearchCriteraCV.Ssn']").val() + '", ' +
        '"StudId": ' + $("input[name='StudentSearchCriteraCV.StudId']").val() + ', ' + //assuming StudID is a number
        '"Sex": "' + $("input[name='StudentSearchCriteraCV.Sex']").val() + '", ' +
        '"Race": "' + $("input[name='StudentSearchCriteraCV.Race']").val() +'" ' +
        '}]}';