在没有响应的情况下从ajax调用操作

时间:2018-04-23 11:16:28

标签: c# ajax asp.net-mvc

下面是我在控制器A中用来调用控制器B中的动作的ajax。它确实向动作发送了相应的参数但是它的视图没有渲染,我怀疑它会返回视图。

Index.cshtml(控制器A)

<script>
    //some code
     if(isAtt){
         var moment = $('#calendar').fullCalendar('getDate');
         alert("The current date of the calendar is " + moment.format());
         var b = $('#calendar').fullCalendar('getDate');
         var id = @Session["currentEmpID"].ToString();
         $.ajax({
              url: baseUrl + 'AttendanceLogs/Index?EnrollNumber=' + id + '&StartDate=' + b.format('YYYY-MM-DD'),
              data: '',
          });
     }
</script>

ControllerB:

public ActionResult Index(int? Page, int? EnrollNumber, DateTime? StartDate)
{
    if (EnrollNumber != null && StartDate != null)
    {
        ViewBag.singleRec = "singleRec";
        TempData["enrollNum"] = EnrollNumber;
        TempData["StartDate"] = StartDate;
        var empId = db.Emps.Where(x => x.EnrollNumber == EnrollNumber.ToString()).Select(x => x.EmployeeId).FirstOrDefault();
        var attLogs = db.AttLogs.Where(x => x.EmpID == empId && x.Date == StartDate).ToList().ToPagedList(Page ?? 1, 3);
        return View(attLogs);
    }
    else
    {
        // some more code
    }
}

1 个答案:

答案 0 :(得分:0)

如果目的只是重定向,则不需要Ajax;

而不是:

 $.ajax({
              url: baseUrl + 'AttendanceLogs/Index?EnrollNumber=' + id + '&StartDate=' + b.format('YYYY-MM-DD'),
              data: '',
          });

试试这个:

window.location.href = baseUrl + "AttendanceLogs/Index?EnrollNumber=" + id + "&StartDate=" + b.format('YYYY-MM-DD');