MVC4中的ActionLink无法解决控制器中的Action

时间:2014-03-28 12:51:55

标签: asp.net-mvc asp.net-mvc-4

当我想要关联控制器的操作时,我有一个学生控制器,其中包含一些操作,我的观点无法找到我的操作名称。

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

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

无法找到 ScheduleStudentList 操作。它只是找到索引删除编辑操作&# 39; s。我该怎么办?

祝你好运

3 个答案:

答案 0 :(得分:3)

正如Mauro所说,你没有指定控制器。但是,当您提供控制器名称时,您需要额外的,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
)

基本上,您的“Student”将进入routevalues参数,而不是被解释为字符串。生成的链接可能会在它的末尾有?length=7,因为它误解了这个参数。

添加其他,null会对其进行更改,以便您拨打this overload

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

答案 1 :(得分:1)

试试这个......

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

您不必指定“Controller”关键字。希望它有所帮助!

查看this帖子

答案 2 :(得分:0)

您的代码存在的问题是您没有使用@Html.ActionLink()的任何有效重载。

试试:

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

正如其他人所建议的那样,它应该有效!

相关问题