为什么ajax结果没有显示在子视图中?

时间:2014-06-01 02:33:38

标签: javascript jquery ajax asp.net-mvc-3 razor

我正在做这样的ajax调用:

 @Ajax.ActionLink("My Schedule", "GetSchedule", "Schedule", new { selectedDate = strToday}, new AjaxOptions { UpdateTargetId = "theTimes", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }) 

它调用一个包含数据贴纸的局部视图。这是控制器操作的样子:

   public ActionResult GetSchedule(string selectedDate)
    {

        ViewBag.fromGetSchedule = selectedDate; 
        var viewModel = new ScheduleData();


        String dayofweek = DateTime.Now.DayOfWeek.ToString();
    viewModel.Courses = db.Courses;

    int intTeacherId = Convert.ToInt32(Session["intTeacherId"]);

        DateTime daySearch = Convert.ToDateTime(selectedDate);
        viewModel.Assignments = from a in db.Assignments where a.teacherId == intTeacherId select a;





    viewModel.Assignments = (from x in db.Assignments where x.teacherId == intTeacherId && x.Date == daySearch select x).ToList();



    ViewBag.theDate = daySearch.ToShortDateString();



        return PartialView(viewModel);

    }

}

一切正常。它调用的部分视图如下所示:

 @model SchoolIn.ViewModels.ScheduleData

  @{
    ViewBag.Title = "Index";
    }


 <link href="../../Content/themes/base/jquery-ui-1.10.4.custom.css" rel="stylesheet"
type="text/css" />

  <link href="../../Content/themes/base/jquery-ui-1.10.4.custom.min.css" rel="stylesheet"
type="text/css" />

  <script src="../../Scripts/jquery-1.10.2.js" type="text/javascript"></script>
   <script src="../../Scripts/jquery-ui-1.10.4.custom.js" type="text/javascript"></script>

   <script src="../../Scripts/jquery-ui-1.10.4.custom.min.js" type="text/javascript"></script>

  <div class ="ui-widget" >
  <label for ="datep">Date: </label><input id="datep" />



  </div>
  @ViewBag.theDate

  @if (Model.Assignments != null)
  {

   <table>
     <tr>
   <th>
      Assignment  Course
    </th>



    <th>
        Class Day
    </th>


    <th>
     Class Time
    </th>


</tr>

@foreach (var item in Model.Assignments.Select((x, i) => new { Data = x, Index = i }))
  {


<tr>
<td>

    @Html.DisplayFor(modelItem => item.Data.Course)

</td>


    <td>





         @Html.DisplayFor(modelItem => item.Data.HomeworkGrade)

    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Data.Date)
    </td>




</tr>
}


 </table>

 }


<input type="hidden" autofocus=true/>




  <script type="text/javascript">
    $(document).ready(function () {
    $("#datep").datepicker({ showOn: "both", buttonText: "Select Date", changeMonth: true, changeYear: true, yearRange: "-2:+2", showOtherMonths: true, onSelect: function (date, datepickder) {
        var sltdDate = { selectedDate: date };
        $.ajax({
            type: "GET",
            url: "/Schedule/GetSchedule",
            data: sltdDate,
            datatype: "html",
            sucess: function (data) {

            }
        });

    }




        });

      });
  </script>

然后在局部视图中,我从datepicker中选择一个日期,并且对我之前提到的相同动作进行ajax调用。再次linq查询进展顺利,一切都很好。我检查视图中的代码,看它是否正在接收使用断点从控制器发送的数据,并且数据确实通过,但它没有显示在视图中。它显示了表的标题,但在@ Html.Foreach(){}循环中没有任何内容。

为什么ajax结果未显示在子视图中?再次感谢您对此提供任何帮助。

1 个答案:

答案 0 :(得分:0)

首先,我需要纠正错字...成功的成功然后我将视图包装在div容器中,然后将返回的数据放入div容器中,并使用success函数。这是成功函数的样子。

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

            }