2014-03-28 67 views
2

我有一個學生控制器,當我想要鏈接控制器的動作時,我的視圖無法找到我的動作名稱。MVC4中的ActionLink無法解決控制器中的操作

所以你可以看到我的控制器的動作:

namespace EducationMVC.Controllers 
{ 
    [Authorize] 

    public class StudentController : Controller 
    { 
     // 
     // GET: /Student/ 
     private StudentRepositor obj = new StudentRepositor(); 

     public ActionResult Index() 
     { 
      var model = obj.GetStudentlist(); 

      return View(model); 
     } 
     public ActionResult ScheduleStudentList(string id) 
     { 
      var model = obj.GetStudentlistOfSchedule(id); 

      return View("Index",model); 
     } 
     public ActionResult Create() 
     { 
      return View("Create"); 

      // return View(); 
     } 
     [HttpPost] 
     public ActionResult Create(Student student) 

     { 
      student.RegisterDate = DateTime.Now.Date; 
      obj.AddNewStudent(student); 
      obj.Save(); 
      return RedirectToAction("Index", "Student"); 
     } 
     public ActionResult Edit(int id) 
     { 

      return View(obj.FindStudentById(id)); 
     } 

     [HttpPost] 
     public ActionResult Edit(Student student) 
     { 

      if (ModelState.IsValid) 
      { 
       obj.Update(student); 
       obj.Save(); 
      } 
      return RedirectToAction("Index", "Student"); 
     } 
     public ActionResult Delete(int id) 
     { 
      obj.RemoveStudent(obj.FindStudentById(id)); 
      obj.Save(); 
      return RedirectToAction("Index", "Student"); 

      // return View(); 
     } 

    } 
} 

這是我的觀點:

@model IEnumerable<EducationMVC.Models.SchedulePresentation> 


@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.teacherName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.lessonName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.className) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.degreeName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.facultyName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.semesterName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.majorName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.DateOfExam) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Capacity) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.examLocation) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.teacherName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.lessonName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.className) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.degreeName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.facultyName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.semesterName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.majorName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.DateOfExam) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Capacity) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.examLocation) 
     </td> 
     <td> 
      @Html.ActionLink("ویرایش", "Edit", new { id=item.id }) | 
      @Html.ActionLink("حذف", "Delete", new { id=item.id })| 
      @Html.ActionLink("لیست دانشجویان ","ScheduleStudentList","StudentController", new { id=item.id }) 


     </td> 
    </tr> 
} 

</table> 

所以你可以在視圖中看到的最後一行:

@Html.ActionLink("لیست دانشجویان ","ScheduleStudentList","StudentController", new { id=item.id }) 

可以't找到ScheduleStudentList action.It只找到index and delete and edit action's .so我該怎麼辦?

問候

+0

在瀏覽器中查看時頁面上生成的鏈接是什麼? –

回答

3

正如毛羅提到的,你沒有指定控制器。但是,當您提供控制器名稱時,您需要額外的,null來清除您要呼叫的超載。

@Html.ActionLink("لیست دانشجویان ", "ScheduleStudentList", "Student", new { id=item.id }, null) 

你得到這個問題,因爲沒有額外的空,你打電話this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    Object htmlAttributes 
) 

從本質上講,你的「學生」是進入了routevalues參數,而不是被解釋爲一個字符串。生成的鏈接可能會在末尾有類似?length=7的內容,因爲它誤解了此參數。

添加額外,null改變它,這樣你打電話this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 
+0

所以當我將它改爲學生,它不起作用,但是當我添加了一些選項,如添加Null,它的工作原理,爲什麼? –

+0

我的問題是什麼? –

+0

更新了答案以包含解釋 – Richard

1

試試這個...

@Html.ActionLink("لیست دانشجویان ","ScheduleStudentList","Student", new { id=item.id }) 

您不必specifie 「控制器」 的關鍵字。希望能幫助到你!

看看this發佈

+0

它不起作用 –

+0

你有第二個參數的路由參數嗎?你是否正確傳遞模型?你應該看看這個方法的定義......可能是簽名有問題。 –

+0

理查德帖子是工作 –

0

與您的代碼的問題是,你不使用的@Html.ActionLink()任何有效的過載。

嘗試:

@Html.ActionLink("لیست دانشجویان ", "ScheduleStudentList", "Student", new { id=item.id }, null)

正如其他人所建議,它應該工作!