如何使用ajax调用加载部分视图?

时间:2022-10-08 13:46:26

I’m trying to load a partial view with an ajax call. I’m trying to follow a solution I found here: Render partial view onclick in asp.net mvc 3 project

我正在尝试用ajax调用加载部分视图。我正在尝试遵循我在这里找到的一个解决方案:在asp.net mvc 3项目中呈现部分视图onclick

My script looks like this:

我的剧本是这样的:

 $("#174").on("click", function () {

        $.ajax({
            type: "GET",
            url: "/TeacherStudent/StudentInformation",

            data: "174",
            datatype: "html",
            sucess: function (data) {
             $("#theTimes").html(result);

            }
        });
    });

My controller looks like this:

我的控制器是这样的:

  public ActionResult StudentInformation(string data)
    {
        int id = Convert.ToInt32(data);


        var viewModel = new ScheduleData();

        var attend = from s in db.Assignments where s.teacherId == 7 && s.StudentId == 174 select s;
        var enroll = from s in db.Enrollments where s.InstructorId == 7 && s.StudentID == 174 select s;
        var stdParent = from s in db.Parents select s;

        viewModel.Assignments = attend;
        viewModel.Enrollments = enroll;
        viewModel.Parents = stdParent;
        return PartialView("~/Views/Shared/DisplayTemplates/StudentInformation.cshtml");
         }

     }

Somewhere in my view I have div tag:

在我的视图中,我有div标签:

 <div id="theTimes"></div>

I confirmed that the program calls the partial view but the view does not get displayed in the div tag. What could I be missing in the ajax call? Thanks for helping out!

我确认程序调用了部分视图,但是视图不会显示在div标签中。在ajax调用中,我可能遗漏了什么?谢谢你的帮忙!

1 个解决方案

#1


3  

The problem looks like it's on this line.

问题看起来是在这条线上。

sucess: function (data) {
    $("#theTimes").html(result);
}

result is undefined and 'sucess' isn't a valid callback.

结果是未定义的,“sucess”不是一个有效的回调。

Try this:

试试这个:

success: function (data) {
    $("#theTimes").html(data);
}

Also, change datatype to dataType (grammar) and change data: "174" to data: {"data": "174"}.

此外,将数据类型更改为数据类型(语法),并将数据“174”更改为数据:{“数据”:“174”}。

I.e.

即。

$("#174").on("click", function () {
    $.ajax({
        type: "GET",
        url: "/TeacherStudent/StudentInformation",
        data: {"data": "174"},
        dataType: "html",
        success: function (data) {
         $("#theTimes").html(data);

        }
    });
});

#1


3  

The problem looks like it's on this line.

问题看起来是在这条线上。

sucess: function (data) {
    $("#theTimes").html(result);
}

result is undefined and 'sucess' isn't a valid callback.

结果是未定义的,“sucess”不是一个有效的回调。

Try this:

试试这个:

success: function (data) {
    $("#theTimes").html(data);
}

Also, change datatype to dataType (grammar) and change data: "174" to data: {"data": "174"}.

此外,将数据类型更改为数据类型(语法),并将数据“174”更改为数据:{“数据”:“174”}。

I.e.

即。

$("#174").on("click", function () {
    $.ajax({
        type: "GET",
        url: "/TeacherStudent/StudentInformation",
        data: {"data": "174"},
        dataType: "html",
        success: function (data) {
         $("#theTimes").html(data);

        }
    });
});