2016-09-28 41 views
0

這裏,ActionLink的在TeacherIndex鑑於TeacherController「讓學生」必須調用StudentController和行動方法StudentList,然後進入查看StudentIndexAjax.ActionLink不調用控制器動作和視圖

TeacherIndex.cshtml在TeacherController:

@model IEnumerable<StudentMVC.Models.TeacherEntity> 

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<table class="table"> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.TeacherId) 
    </th> 
    ...  
</tr> 
@if (Model != null) 
{ 

foreach (var item in Model) 
{ 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.TeacherId) 
    </td> 
    ... 
    <td>    
     @Ajax.ActionLink("Get Students", "StudentList", "Student", new { id = item.TeacherId }, new AjaxOptions { HttpMethod = "POST" })   
    </td> 
</tr> 
} 
} 

StudentController:

[HttpPost] 
public ActionResult StudentList(int TeacherId) 
    { 
     List<StudentEntity> studentList = new List<StudentEntity>(); 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Moviedb"].ConnectionString);    
     SqlCommand cmd = new SqlCommand("SP_Student", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@TeacherId", TeacherId); 
     con.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     while (dr.Read()) 
     { 
      StudentEntity student = new StudentEntity(); 
      student.StudentId = Convert.ToInt32(dr["StudentId"]); 
      //...    
      studentList.Add(student); 
     } 
     con.Close(); 
     return View("StudentIndex", studentList); 
    } 

但在這裏,取而代之的是,顯示

'/'應用程序中的服務器錯誤。無法找到該資源。

請求的URL:/學生/ StudentList/1

回答

3

參數的名稱應該是相同的。使用這個:

@Ajax.ActionLink("Get Students", "StudentList", "Student", new { @TeacherId = item.TeacherId }, new AjaxOptions { HttpMethod = "POST" }) 
+0

我已經嘗試過上面的代碼。但錯誤沒有改變。 –

相關問題