如何使用ajax调用加载我的局部视图?

时间:2014-04-28 04:04:04

标签: ajax asp.net-mvc

我正在尝试使用ajax调用加载部分视图。我正在尝试按照我在此处找到的解决方案:Render partial view onclick in asp.net mvc 3 project

我的脚本如下所示:

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

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

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

            }
        });
    });

我的控制器看起来像这样:

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

     }

在我看来,我有div标签:

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

我确认程序调用了局部视图,但视图没有显示在div标签中。在ajax调用中我可能缺少什么?谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

问题看起来就像是在这一行。

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

result未定义且“成功”不是有效的回调。

试试这个:

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

另外,将datatype更改为dataType(语法)并将data: "174"更改为data: {"data": "174"}

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

        }
    });
});