Ehsan Akbar
Ehsan Akbar

Reputation: 7281

ActionLink in MVC4 doesn't solve the Action in controller

I have a student controller with some actions when i want to link my controller's action ,my view can't find my action's name.

So you can see my controller's action :

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

    }
}

Here is my 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>

So as you can see in view the last line :

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

can't find ScheduleStudentList action.It just find index and delete and edit action's .so what should i do ?

Best regards

Upvotes: 2

Views: 1425

Answers (3)

Richard
Richard

Reputation: 30618

As Mauro mentioned, you don't specify controller. However, when you supply the controller name, you need an additional ,null to be clear which overload you're calling.

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

You're getting this problem because without the extra null, you're calling this overload:

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

Essentially, your "Student" is going into the routevalues parameter, rather than being interpreted as a string. The generated link probably would've had something like ?length=7on the end of it, as it misinterpreted this parameter.

Adding the additional ,null changes it so that you're calling this overload:

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

Upvotes: 3

Aritra B
Aritra B

Reputation: 1746

The problem with your code is that you are not using any valid overload of @Html.ActionLink().

Try :

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

as others have suggested and it should work!

Upvotes: 0

Mauro Bilotti
Mauro Bilotti

Reputation: 6232

try this...

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

You don't have to specifie the "Controller" keyword. Hope it helps!

Take a look at this post

Upvotes: 1

Related Questions