当前位置 : 主页 > 网页制作 > JQuery >

jquery – 在MVC Razor View中调用AJAX

来源:互联网 收集:自由互联 发布时间:2021-06-15
我刚刚注意到一种我不太确定的奇怪行为.我是jQuery和 AJAX的新手.所以可能只是缺少一些基础知识. 在我的MVC rzaor视图中,有一些AJAX Action链接,当点击时会在div #StudentDetails中呈现包含Stud
我刚刚注意到一种我不太确定的奇怪行为.我是jQuery和 AJAX的新手.所以可能只是缺少一些基础知识.

在我的MVC rzaor视图中,有一些AJAX Action链接,当点击时会在div #StudentDetails中呈现包含Student Details的部分视图.

<div id="mainpage">

    <h2>Registration Details</h2>
    <ul>
    @foreach(var item in Model) 
    {
        <li>
            @Ajax.ActionLink(item.Student.UserName, @*Text to be displayed *@
            "GetUserDetails", @*Action Method Name*@
            new { id = item.Student.StudentId },
                new AjaxOptions
                {
                    UpdateTargetId = "StudentDetails", @*DOM element ID to be updated *@
                    InsertionMode = InsertionMode.Replace,@*Replace the content of DOM element *@
                    HttpMethod = "GET" @*HTTP method *@,
                    OnComplete="RegisterClickHanders"
                }
            )
        </li>
    }
    </ul>
    <div id ="StudentDetails"></div>
    <br />
    <div id ="Add_Update_Details"></div>
</div>

使用详细信息渲染视图后,可以单击“编辑”或“添加”按钮,并呈现相应的部分视图.

<script>
//Event Delegation
//$(function () {
//One can also use Event Delegation as stated above to handle dynamically added elements i.e. the elements
//that are added after the page is first loaded.
function RegisterClickHanders() {

    var url = '@Url.Action("DisplayClickedView","Private")'; // Name of the action method you want to call and the name of the 
    //controller the action method resides
            $('.editDetails').click(function () {
                var btnvalue = $('.editDetails').attr("value");
                var studentId = $('.editDetails').data("student-id");

                        $('#Add_Update_Details').load(url, { searchText: btnvalue, searchValue: studentId });
            });

            $('.addDetails').click(function () {
                var btnvalue = $('.addDetails').attr("value");

                $('#Add_Update_Details').load(url, { searchText: btnvalue });
            });

}
</script>

在我的控制器中,当我提出断点时:

他们只打了一次打算.但是当我将ajax调用替换为:

$('.editDetails').click(function () {
        var btnvalue = $('.editDetails').attr("value");
        var studentId = $('.editDetails').data("student-id");

        $.ajax({
            type: 'POST',
            url: url,
            data: { searchText: btnvalue, searchValue: studentId },
            success: function () {
                $('#Add_Update_Details').load(url, { searchText: btnvalue, searchValue: studentId });
            },
            error: function (jqXHR, status, err) {//status is Error and the errorThrown is undefined
                alert('Request Status : ' + jqXHR.status + ' has issued a status text of  : ' + jqXHR.statusText);
            }
        });
    });



    $('.addDetails').click(function () {
        var btnvalue = $('.addDetails').attr("value");

        $.ajax({
            type: 'POST',
            url: url,
            data: { searchText: btnvalue },
            success: function () {
                $('#Add_Update_Details').load(url, { searchText: btnvalue });
            },
            error: function (jqXHR, status, err) {//status is Error and the errorThrown is undefined
                alert('Request Status : ' + jqXHR.status + ' has issued a status text of  : ' + jqXHR.statusText);
            }
        });

    });

按下任何按钮后,控制器中的断点将被击中两次.我错过了什么?或者这是打算? AJAX呼叫是否会导致性能下降?

.load和你的ajax调用都在调用你的控制器动作.

您应该返回一个PartialView并将其写入空div,如下所示:

$.ajax({
    type: 'POST',
    url: url,
    data: { searchText: btnvalue, searchValue: studentId },
.success(function (result) {
    $('#Add_Update_Details').html(result);
})
网友评论